---
title: "Master File: All Stata and R Scripts"
author: "Valentina Gonzalez Rostani"
date: "2024-08-24"
output:
  html_document:
    toc: true          # Enables table of contents in HTML
    toc_depth: 5       # Sets the depth of the table of contents to 5
  pdf_document:
    toc: true          # Enables table of contents in PDF
    toc_depth: 5       # Sets the depth of the table of contents in PDF
---



```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

#library(reticulate) # to interact R & py

```

```{r, include=FALSE}
library(Statamarkdown) # to run stata
library(kableExtra)

```

```{stata, include=FALSE, echo=FALSE}
** Set directory
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication"

```


# Overvie of this file


This file contains most of the code and data needed to replicate results in Gonzalez-Rostani (2024) "Elections, Right-wing Populism, and Political-Economic Polarization: The Role of Institutions and Political Outsiders." The structure of the file is based on each one of the files (scripts, do-files, rmd) that can be found in the folder `do`. All of the replication instructions assume that you set the working directory to the same folder as the `Master.rmd` or `Master.pdf`` files. 


How this file is organized?

- Each section refers to a code file. 
- At the beginning the inputs, and outputs are explained. 
- Subsections are used to refer to the number/title of the Figure or Table generated. 
- This directory contains all the code used in the analysis. The files are categorized based on their relevance to different sections of the study:
    - Files starting with `1_` correspond to analyses related to the **Vote-switching across Institutions** section.
    - Files starting with `2_` pertain to the **Messaging and Targeting Strategies: Candidate Rhetoric and Party Platforms** section, specifically focusing on **Majoritarian Systems** (e.g., US analyses).
    - Files starting with `3_` also relate to the **Messaging and Targeting Strategies** section but focus on **PRITM Systems**.
    - Files starting with `4_` contain code for **Figures that provide context and additional details** but do not directly reflect results.


**NOTE**: This code does not include files jupyter notebooks:

* `2_0_Speech_US_dictionaries.ipynb`
* `2_3_Speech_US_Germany_Appendix_NMF.ipynb`

Refer to these files directly in the `do` folder.

If you prefer to see each file individually go to the folder `do`.


# Motivation Figures [4_1_Figures_ISSP.do]


This do-file:

- Creates Figure 1, 2 and A3 using data from ISSP. 

Input:

- `Data\Figures_ISSP.dta`

Output:

- Figure 1: Relative Share of Labor Force 1995 to 2014 [`Figures\Relative Share of Labor Force 1998 to 2014 ISSP.pdf`]
- Figure 2: Electoral consequences, Routine and Non-Routine Voters [`Figure/price_by_mpg.pdf`]
- Figure A3: Importance of job security, Difficulties to find a new job, Concerns about losing the job and Job dissatisfaction [`Figure/jobdisatisfactionpredictedtogetherall.pdf`]



```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Load  data
*##########################################

use "Data\Figures_ISSP.dta", clear 
	
```



### Figure 1: Relative Share of Labor Force 1995 to 2014

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*******************************************************************************
* Graphs
*******************************************************************************
* Graph style for F1 & 2
{
grstyle clear
set scheme s2color
grstyle init
grstyle set plain, box
grstyle color background white
grstyle color major_grid gs8
grstyle linepattern major_grid dot
}
// Figure 1: Relative Share of Labor Force 1995 to 2014
{
    * Generating summary statistics with three task categories:
    * Task 1, Task 2, and Task 33 following Autor (2003) and coded by Kurer and Gallego (2019)
    
    * Preserve the current dataset in memory so it can be restored later
    preserve 
    
    * Collapse the data to calculate the mean of task1, task2, and task33 weighted by 'weight' for each year
    collapse task1 task2 task33 [aw=weight], by(year)
    
    * Keep only the observations where the year is greater than 1997 to look post automation shock
    keep if year > 1997
    
    * Create a line graph for the task categories over time
    * The first line represents Non-Routine Cognitive tasks (green line)
 graph twoway line  task1 year if year>1997, lc(green) legend(label(1 "Non-Routine Cognitive")) || ///
  line  task2 year if year>1997, lc(red) legend(label(2 "Routine")) || ///
    line  task33 year if year>1997, lc(blue) legend(label(3 "Non-Routine Manual")) 

    
    * Export the graph as a PDF file with the specified name, replacing any existing file
    graph export "Figure\RelativeShareofLaborForce1998to2014ISSP.pdf", as(pdf) replace
    
    * Restore the original dataset that was in memory before the collapse
    restore
}
```




![Figure 1: Relative Share of Labor Force 1995 to 2014](Figure\\RelativeShareofLaborForce1998to2014ISSP.pdf){ width=12cm }



### Figure 2: Electoral consequences, Routine and Non-Routine Voters

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
// Figure 2: Electoral consequences, Routine and Non-Routine Voters
{
* Here I look at the share of votes by party family, and instead of using the three categories of risks, I put together non-routine manual and routine. I repeat this for RR, ML, MR, Non-voters. Code commented in the first one. 
// Radical right (graph commented)
{
* Label the variable 'radicalR' to represent "Votes for Right Populist (%)"
lab var radicalR "Votes for Right Populist (%)"

* Preserve the current dataset in memory so it can be restored later
preserve 	

* Generate a new variable 'task2and3' initialized to 1 if 'task2' equals 1
gen task2and3 = 1 if task2 == 1

* Replace 'task2and3' with 1 if 'task33' equals 1 (combining task2 and task33)
replace task2and3 = 1 if task33 == 1

* Replace 'task2and3' with 0 if 'task1' equals 1 (Non-Routine Cognitive tasks)
replace task2and3 = 0 if task1 == 1

* Keep only observations where 'emplB' is less than 6 (likely filtering by employment status or category)
keep if emplB < 6

* Collapse the data to calculate the mean of 'radicalR' weighted by 'weight' for each year, task1, and task2and3 categories
collapse radicalR [aw=weight], by(year task1 task2and3)

* Create a line graph for 'radicalR' over time, segmented by task categories
graph twoway line  radicalR year if task1==1, lc(green) legend(label(1 "Non-Routine Cognitive"))  || ///
  line  radicalR year if task2and3==1, lc(red) legend(label(2 "Routine & Manual")) ytitle("Votes for Right Populist (%)", size(small))    legend(size(vsmall))  xtitle("Year", size(small))   ylabel(, labsize(vsmall))  xlabel(, labsize(vsmall))

* Save the graph as "RR.gph" in the "Figure" directory, replacing any existing file with the same name
graph save "Figure/RR.gph", replace

* Restore the original dataset that was in memory before the modifications
restore
}
// Maintream left
{
 preserve 	

gen task2and3=1 if task2==1
replace task2and3=1 if task33==1
replace task2and3=0 if task1==1

keep if emplB<6
collapse mainstreamleft [aw=weight], by(year task1 task2and3)

graph twoway line  mainstreamleft year if task1==1, lc(green)   legend(label(1 "Non-Routine Cognitive"))  || ///
  line  mainstreamleft year if task2and3==1, lc(red) legend(label(2 "Routine & Manual"))  ytitle("Votes for Mainstream Left (%)", size(small))    legend(size(vsmall))  xtitle("Year", size(small))   ylabel(, labsize(vsmall))  xlabel(, labsize(vsmall))
graph save "Figure/ML.gph", replace

restore	
}
// Mainstream right
{
 preserve 	

gen task2and3=1 if task2==1
replace task2and3=1 if task33==1
replace task2and3=0 if task1==1

keep if emplB<6
collapse mainstreamright [aw=weight], by(year task1 task2and3)

graph twoway line  mainstreamright year if task1==1, lc(green)  legend(label(1 "Non-Routine Cognitive"))  || ///
  line  mainstreamright year if task2and3==1, lc(red) legend(label(2 "Routine & Manual"))  ytitle("Votes for Mainstream Right (%)", size(small))    legend(size(vsmall))  xtitle("Year", size(small))   ylabel(, labsize(vsmall))  xlabel(, labsize(vsmall))
graph save "Figure/MR.gph", replace

restore	
}
// Non Voters
{
 preserve 	

gen task2and3=1 if task2==1
replace task2and3=1 if task33==1
replace task2and3=0 if task1==1

keep if emplB<6
collapse nonvoters [aw=weight], by(year task1 task2and3)

graph twoway line  nonvoters year if task1==1, lc(green)  legend(label(1 "Non-Routine Cognitive"))  || ///
  line  nonvoters year if task2and3==1, lc(red) legend(label(2 "Routine & Manual"))   ytitle("Non-Voters (%)", size(small))    legend(size(vsmall))  xtitle("Year", size(small))   ylabel(, labsize(vsmall))  xlabel(, labsize(vsmall))
graph save "Figure/Nv.gph", replace

restore	
}
 * Combine multiple graphs into a single figure
graph combine "Figure/ML.gph" "Figure/MR.gph" "Figure/RR.gph" "Figure/Nv.gph"
* Export the combined graph as a PDF file named "price_by_mpg.pdf" in the "Figure" directory
graph export "Figure/price_by_mpg.pdf", as(pdf) replace

* Erase (delete) the individual graph files from the "Figure" directory after combining them
erase "Figure/ML.gph"
erase "Figure/MR.gph"
erase "Figure/RR.gph"
erase "Figure/Nv.gph"



}
```





![Figure 2: Electoral consequences, Routine and Non-Routine Voters](Figure\\price_by_mpg.pdf){ width=12cm }



# Vote-switching across Institutions

## US [1_1_Switching_US.do]


* This do-file:
	* Processing of the data
		* Call the Data
		* Define variables
		* Save the data
	* Load preapred data **line 794**
	* Analysis and Descriptives: Export Tables &  Figure

Input: GSS data
	- `Data\Switching\GSS7218_R3.dta`

Final output:

* Cleaned data: 
  * `Data\GSS.dta` this data contains the relevant variables for the analysis.
* Tables:
	* table A3: Switching Vote, IV - RTI, US [``Table\USlong_2.tex``]
	* table A5: Switching Vote (alternative definition), IV - RTI [`Table\USStrict_2.tex`]
	* table A6: Switching Vote, IV - Routine (dummy), US [`Table\USdummy_2.tex`]
	* table A1: Descriptive statistic: USA GSS 2016 vs 2012 [`Table/summarystats_US.tex`]
* Figure:
	* Figure 3: The effect of exposure to automation on vote-switching. [US Part]  [`Figure\US.gph`]
	
	
First, you will find the code to prepare the data (hidden in the pdf). 

```{stata, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE, collectcode=TRUE}
clear mata
clear matrix
*##########################################
* Processing of the data (alternatively skip and go to line 791)
*##########################################
{
*##########################################
* Calling the data
*##########################################
{
* Clearing and allowing multiple variables
clear
set maxvar 30000

** Set directory
**cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication"


* Calling the data
use "Data\Switching\GSS7218_R3.dta", clear
}
```

```{stata, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE, collectcode=TRUE}

*############################################
* Creating Variables
*############################################
{
************************************************************************
********************* B. Demographic  **********************************
************************************************************************
{
* Generate a dummy variable for unemployment status
gen unemployed = (wrkstat==4)

* Generate a dummy variable for female
gen female=(sex==2)

* Generate a dummy variable for black race
gen black = (race==2)

* Dummy for born outside the US
gen foreign = .  // Initialize the variable 'foreign' with missing values
replace foreign=1 if born==2
replace foreign=0 if born==1

* Dummy for nonreligion
gen nonrelig=.
replace nonrelig=1 if relig==4
replace nonrelig=0 if relig~=4 & relig~=.

}
************************************************************************
********************* C. Standarizing occupations **********************
************************************************************************
{
*/ In this section I will standarize occupations. I follow Thewissen and Rueda (2019)  code, which was based on Harry Ganzeboom's correspondence file, who used ILO correspondence table.

* using code from Harry Ganzeboom, I convert occupations from ISCO-08 to ISCO-88. 
* I am doing this conversion because the RTI is in 08 ISCO code. 
gen iscoco = isco88

*/ Given that Goos et al (2014) code is designed for 2-digit. I create a new variable that will transform iscoco into a 2 digit variables. 

foreach var of varlist iscoco isco08 {
gen str_`var'=string(`var')
replace str_`var'="0100" if str_`var'=="100"
}
sum iscoco if str_iscoco=="0100"


foreach x in iscoco {
gen `x'2 = substr(str_`x',1,2) if `x'<9334
replace `x'2 = "." if `x'2==""
}

drop str_*

destring iscoco2, replace

label var iscoco2 "ISCO88 at the 2-digit"

* Labels Iscoco
{
gen iscoco2n="."
replace iscoco2n="Legislators and senior officials" if iscoco2==11
	replace iscoco2n="Corporate managers" if iscoco2==12
	replace iscoco2n="General managers" if iscoco2==13
	replace iscoco2n="Physical, mathematical and engineering science professionals" if iscoco2==21
	replace iscoco2n="Life science and health professionals" if iscoco2==22
	replace iscoco2n="Teaching professionals" if iscoco2==23
	replace iscoco2n="Other professionals" if iscoco2==24
	replace iscoco2n="Physical and engineering science associate professionals" if iscoco2==31
	replace iscoco2n="Life science and health associate professionals" if iscoco2==32
	replace iscoco2n="Teaching associate professionals" if iscoco2==33
	replace iscoco2n="Other associate professionals" if iscoco2==34
	replace iscoco2n="Office clerks" if iscoco2==41
	replace iscoco2n="Customer services clerks" if iscoco2==42
	replace iscoco2n="Personal and protective services workers" if iscoco2==51
	replace iscoco2n="Models, salespersons and demonstrators" if iscoco2==52
	replace iscoco2n="Market-oriented skilled agricultural and fishery workers" if iscoco2==61
	replace iscoco2n="Subsistence agricultural and fishery workers" if iscoco2==62
	replace iscoco2n="Extraction and building trades workers" if iscoco2==71
	replace iscoco2n="Metal, machinery and related trades workers" if iscoco2==72
	replace iscoco2n="Precision, handicraft, printing and related trades workers" if iscoco2==73
	replace iscoco2n="Other craft and related trades workers" if iscoco2==74
	replace iscoco2n="Stationary-plant and related operators" if iscoco2==81
	replace iscoco2n="Machine operators and assemblers" if iscoco2==82
	replace iscoco2n="Drivers and mobile-plant operators" if iscoco2==83
	replace iscoco2n="Sales and services elementary occupations" if iscoco2==91
	replace iscoco2n="Agricultural, fishery and related labourers" if iscoco2==92
	replace iscoco2n="Labourers in mining, construction, manufacturing and transport" if iscoco2==93
	replace iscoco2n="Armed forces" if iscoco2==01
label var iscoco2n "Names of iscoco2 coding"
}





************************************************************************
********************* Exposure to automation ***************************
************************************************************************
{
************************************************************************
* 1. Goos et al.(2014)
************************************************************************
{
** These lines include the routinisation and offshoring indices at the 2-digit level from Goos et al. (2014)

*/ The idea is that for each occupation (at the 2-digit level) one risk is assigned. The weak part of this index is that it treats as the same the last 2-digit of the ISCO.
* In other words, ISCO has 4 digit, but goos et al only consider the first 2 digit, therefore there is no differentiation among occupations that differ in the last 2 digit. 
gen rti=.

{
replace rti=-0.75 if iscoco2==12
replace rti=-0.82 if iscoco2==21
replace rti=-1.00 if iscoco2==22
replace rti=-0.73 if iscoco2==24
replace rti=-1.52 if iscoco2==13
replace rti=-0.40 if iscoco2==31
replace rti=-0.44 if iscoco2==34
replace rti=-0.33 if iscoco2==32

replace rti=0.32 if iscoco2==81
replace rti=0.46 if iscoco2==72
replace rti=-1.50 if iscoco2==83
replace rti=2.24 if iscoco2==41
replace rti=1.59 if iscoco2==73
replace rti=-0.19 if iscoco2==71
replace rti=1.41 if iscoco2==42
replace rti=0.49 if iscoco2==82
replace rti=1.24 if iscoco2==74

replace rti=0.45 if iscoco2==93
replace rti=-0.60 if iscoco2==51
replace rti=0.05 if iscoco2==52
replace rti=0.03 if iscoco2==91
}
label var rti "RTI index"

}
************************************************************************
***** 2. Oesch (2006) and Kitschelt and Rehm (2014) classification *****
************************************************************************
{
*Now is the same, but using Oesch (2006) and Kitschelt and Rehm (2014) classification

*/  Oesch (2006) develops the bases of a new class schema that partly shifts its focus from hierarchical divisions to horizontal cleavages. The idea is  that the middle class is not conceptualized as a unitary grouping and the manual/non-manual divide is not used as a decisive class boundary. 
*/ The emphasis is put on differences in marketable skills and the work logic. 

// What is the logic? 
*/Logic of task structures: t1 organizational (taskorg), t2 technical (tasktech), and t3 interpersonal (taskinter)
*/Logic of authority: a1 professional (authprof), a2 associate professional (authassoc), a3 skilled routine (authskil) a4 unskilled routine (authunsk)
*/Leads to 3*4 groups, in regressions of K&R combined to 4 groups (skilled+unskilled routine all tasks; prof+assoc prof for 3 tasks separately)

* Code from Thewissen and Rueda  (2019)
{
*/t1a1: Higher grade managers
gen t1a1=1 if iscoco>=1000 & iscoco<=1251 | iscoco>=2410 & iscoco<=2419 | inlist(iscoco,2441,2470)
label var t1a1 "Higher grade managers"

*/t1a2: Associate managers
gen t1a2=1 if iscoco>=1252 & iscoco<=1319 | iscoco>=3410 & iscoco<=3449 | inlist(iscoco,3452)
label var t1a2 "Associate managers"

*/t1a3: Skilled office
gen t1a3=1 if iscoco>=4000 & iscoco<=4112 | iscoco>=4114 & iscoco<=4141 | inlist(iscoco,4143) | iscoco>=4190 & iscoco<=4210 | iscoco>=4213 & iscoco<=4221
label var t1a3 "Skilled office"

*/t1a4: Routine office
gen t1a4=1 if inlist(iscoco,4113,4142,4144) | iscoco>=4211 & iscoco<=4212 | iscoco>=4222 & iscoco<=4223
label var t1a4 "Routine office"

*/t2a1: Technical experts
gen t2a1=1 if iscoco>=2100 & iscoco<=2213
label var t2a1 "Technical experts"

*/t2a2: Technicians
gen t2a2=1 if iscoco>=3100 & iscoco<=3213 | inlist(iscoco,3471)
label var t2a2 "Technicians"

*/t2a3: Skilled crafts
gen t2a3=1 if inlist(iscoco,110,8311,8324,8333) | iscoco>=7120 & iscoco<=7142 | iscoco>=7200 & iscoco<=7233 | iscoco>=7240 & iscoco<=7423 | iscoco>=7430 & iscoco<=7520
label var t2a3 "Skilled crafts"

*/t2a4: Routine operatives/agriculture
gen t2a4=1 if inlist(iscoco,7143, 7234, 7424, 8312) | iscoco>=7100 & iscoco<=7113 | iscoco>=7129 & iscoco<=7130 | iscoco>=8000 & iscoco<=8310 | iscoco>=8334 & iscoco<=8400 | iscoco>=9160 & iscoco<=9162 | iscoco>=9300 & iscoco<=9333
replace t2a4=1 if iscoco>=6010 & iscoco<=6210 | iscoco>=8330 & iscoco<=8332 | iscoco>=9200 & iscoco<=9213
label var t2a4 "Routine operatives/agriculture"

*/t3a1: Socio-cultural professionals
gen t3a1=1 if inlist(iscoco, 2359, 2445, 2451, 2460) | iscoco>=2220 & iscoco<=2323 | iscoco>=2350 & iscoco<=2351 |iscoco>=2420 & iscoco<=2440 | iscoco>=2442 & iscoco<=2443
label var t3a1 "Socio-cultural professionals"

*/t3a2: Socio-cultural semi-professionals
gen t3a2=1 if inlist(iscoco,2352, 2444, 3220, 3226) | iscoco>=2330 & iscoco<=2340 | iscoco>=2446 & iscoco<=2450 | iscoco>=2452 & iscoco<=2455 | iscoco>=3222 & iscoco<=3224 | iscoco>=3229 & iscoco<=3232 | iscoco>=3240 & iscoco<=3400 | iscoco>=3450 & iscoco<=3451 | iscoco>=3460 & iscoco<=3470 | iscoco>=3472 & iscoco<=3480
label var t3a2 "Socio-cultural semi-professionals"

*/t3a3: Skilled service
gen t3a3=1 if inlist(iscoco, 3221, 3225, 5122, 5141, 5143, 8323) | iscoco>=3227 & iscoco<=3228 | iscoco>=5110 & iscoco<=5113 | iscoco>=5150 & iscoco<=5163 | iscoco>=5200 & iscoco<=5210
label var t3a3 "Skilled service"

*/t3a4: Routine service
gen t3a4=1 if inlist(iscoco,5142, 5149, 5169) | iscoco>=5120 & iscoco<=5121 | iscoco>=5123 & iscoco<=5130 | iscoco>=5131 & iscoco<=5140 | iscoco>=5220 & iscoco<=5230 | iscoco>=8320 & iscoco<=8322 | iscoco>=9100 & iscoco<=9153
label var t3a4 "Routine service"


forvalues i=1(1)3 {
	gen t`i'=1 if t`i'a1==1 | t`i'a2==1  | t`i'a3==1 | t`i'a4==1
	}
list t1-t3 if t1==t2 & t2==t3 & t1==t3 & t1~=.
replace t1=0 if t2==1 | t3==1
replace t2=0 if t1==1 | t3==1
replace t3=0 if t1==1 | t2==1

label var t1 "Organisational task structure (t1a1, 2, 3, or 4==1)"
label var t2 "Technical task structure (t2a1, 2, 3, or 4==1)"
label var t3 "Interpersonal task structure (t3a1, 2, 3, or 4==1)"

forvalues i=1(1)4 {
	gen a`i'=1 if t1a`i'==1 | t2a`i'==1  | t3a`i'==1
	}
replace a1=0 if a2==1 | a3==1 | a4==1
replace a2=0 if a1==1 | a3==1 | a4==1
replace a3=0 if a1==1 | a2==1 | a4==1
replace a4=0 if a1==1 | a2==1 | a3==1

label var a1 "Professional authority (t1a1, t2a1, or t3a1==1)"
label var a2 "Assoc prof authority (t1a2, t2a2, or t3a2==1)"
label var a3 "Skilled routine authority (t1a3, t2a3, or t3a3==1)"
label var a4 "Unskilled routine authority (t1a4, t2a4, or t3a4==1)"

gen check = a1+a2+a3+a4
sum check
drop check

gen c1=1 if t1a1==1 | t1a2==1
gen c2=1 if t2a1==1 | t2a2==1
gen c3=1 if t3a1==1 | t3a2==1
gen c4=1 if t1a3==1 | t1a4==1 | t2a3==1 | t2a4==1 | t3a3==1 | t3a4==1

replace c1=0 if c2==1 | c3==1 | c4==1
replace c2=0 if c1==1 | c3==1 | c4==1
replace c3=0 if c1==1 | c2==1 | c4==1
replace c4=0 if c1==1 | c2==1 | c3==1

label var c1 "Skilled organisational (t1a1 or t1a2==1)"
label var c2 "Skilled technical (t2a1 or t2a2==1)"
label var c3 "Skilled interpersonal (t3a1 or t3a2==1)"
label var c4 "Unskilled routine (t1a3, t1a4, t2a3, t2a4, t3a3, or t3a4==1)"

gen check = c1+c2+c3+c4
sum check
drop check
}
}
*********************************************************************
***** 3. Offshoring index from Walter (2017), based on Blinder*******
*********************************************************************
{
*/ Stefanie Walter (2017) provides the code for offshorability based on Blinder. It uses ISCO with 4 digits. 

gen offshwalt=.
label var offshwalt "Offshoring Potential (Blinder) from Walter"


*4-digit ISCO-code
 * Coding is based on the classification developed in Blinder, Alan. 2007. "How Many U.S. Jobs Might Be Offshorable." CEPS Working Paper No. 142.
* NOTE: all professions not listed by Blinder are coded as not offshorable (value 0)
* Unlike Goos et al, Walter considers the 4 digits. 
{
replace offshwalt=0 if iscoco<.
replace offshwalt=49 if iscoco==1142
replace offshwalt=55 if iscoco==1222
replace offshwalt=28 if iscoco==1226
replace offshwalt=55 if iscoco==1228
replace offshwalt=83 if iscoco==1231
replace offshwalt=49 if iscoco==1232
replace offshwalt=40 if iscoco==1233
replace offshwalt=53 if iscoco==1234
replace offshwalt=49 if iscoco==1235
replace offshwalt=55 if iscoco==1236
replace offshwalt=55 if iscoco==1237
replace offshwalt=55 if iscoco==1311
replace offshwalt=55 if iscoco==1312
replace offshwalt=55 if iscoco==1313
replace offshwalt=55 if iscoco==1314
replace offshwalt=55 if iscoco==1315
replace offshwalt=48 if iscoco==1316
replace offshwalt=52 if iscoco==1317
replace offshwalt=55 if iscoco==1318
replace offshwalt=55 if iscoco==1319
replace offshwalt=66 if iscoco==2111
replace offshwalt=74 if iscoco==2112
replace offshwalt=66 if iscoco==2113
replace offshwalt=66 if iscoco==2114
replace offshwalt=89 if iscoco==2121
replace offshwalt=96 if iscoco==2122
replace offshwalt=83 if iscoco==2131
replace offshwalt=90 if iscoco==2139
replace offshwalt=25 if iscoco==2141
replace offshwalt=64 if iscoco==2143
replace offshwalt=74 if iscoco==2144
replace offshwalt=72 if iscoco==2146
replace offshwalt=69 if iscoco==2147
replace offshwalt=86 if iscoco==2148
replace offshwalt=71 if iscoco==2149
replace offshwalt=81 if iscoco==2211
replace offshwalt=83 if iscoco==2212
replace offshwalt=72 if iscoco==2411
replace offshwalt=50 if iscoco==2419
replace offshwalt=74 if iscoco==2421
replace offshwalt=67 if iscoco==2444
replace offshwalt=90 if iscoco==2451
replace offshwalt=78 if iscoco==2452
replace offshwalt=25 if iscoco==2453
replace offshwalt=48 if iscoco==2455
replace offshwalt=47 if iscoco==3111
replace offshwalt=47 if iscoco==3113
replace offshwalt=47 if iscoco==3114
replace offshwalt=72 if iscoco==3115
replace offshwalt=47 if iscoco==3116
replace offshwalt=94 if iscoco==3118
replace offshwalt=54 if iscoco==3119
replace offshwalt=75 if iscoco==3121
replace offshwalt=84 if iscoco==3122
replace offshwalt=68 if iscoco==3123
replace offshwalt=36 if iscoco==3131
replace offshwalt=36 if iscoco==3132
replace offshwalt=46 if iscoco==3133
replace offshwalt=34 if iscoco==3139
replace offshwalt=52 if iscoco==3141
replace offshwalt=60 if iscoco==3152
replace offshwalt=55 if iscoco==3211
replace offshwalt=55 if iscoco==3212
replace offshwalt=44 if iscoco==3213
replace offshwalt=32 if iscoco==3228
replace offshwalt=51 if iscoco==3411
replace offshwalt=85 if iscoco==3412
replace offshwalt=50 if iscoco==3414
replace offshwalt=55 if iscoco==3416
replace offshwalt=59 if iscoco==3419
replace offshwalt=51 if iscoco==3421
replace offshwalt=48 if iscoco==3422
replace offshwalt=38 if iscoco==3431
replace offshwalt=51 if iscoco==3432
replace offshwalt=84 if iscoco==3433
replace offshwalt=84 if iscoco==3434
replace offshwalt=54 if iscoco==3439
replace offshwalt=100 if iscoco==3442
replace offshwalt=85 if iscoco==3471
replace offshwalt=30 if iscoco==3472
replace offshwalt=95 if iscoco==4111
replace offshwalt=94 if iscoco==4112
replace offshwalt=100 if iscoco==4113
replace offshwalt=71 if iscoco==4114
replace offshwalt=38 if iscoco==4115
replace offshwalt=84 if iscoco==4121
replace offshwalt=54 if iscoco==4122
replace offshwalt=31 if iscoco==4131
replace offshwalt=67 if iscoco==4132
replace offshwalt=67 if iscoco==4133
replace offshwalt=46 if iscoco==4141
replace offshwalt=26 if iscoco==4142
replace offshwalt=95 if iscoco==4143
replace offshwalt=54 if iscoco==4144
replace offshwalt=54 if iscoco==4190
replace offshwalt=94 if iscoco==4211
replace offshwalt=54 if iscoco==4214
replace offshwalt=65 if iscoco==4215
replace offshwalt=72 if iscoco==4221
replace offshwalt=54 if iscoco==4222
replace offshwalt=50 if iscoco==4223
replace offshwalt=86 if iscoco==5113
replace offshwalt=59 if iscoco==6142
replace offshwalt=36 if iscoco==7111
replace offshwalt=35 if iscoco==7112
replace offshwalt=36 if iscoco==7113
replace offshwalt=65 if iscoco==7211
replace offshwalt=69 if iscoco==7212
replace offshwalt=70 if iscoco==7213
replace offshwalt=70 if iscoco==7222
replace offshwalt=68 if iscoco==7223
replace offshwalt=68 if iscoco==7224
replace offshwalt=26 if iscoco==7311
replace offshwalt=64 if iscoco==7313
replace offshwalt=69 if iscoco==7321
replace offshwalt=69 if iscoco==7322
replace offshwalt=68 if iscoco==7323
replace offshwalt=68 if iscoco==7324
replace offshwalt=60 if iscoco==7331
replace offshwalt=75 if iscoco==7332
replace offshwalt=59 if iscoco==7341
replace offshwalt=59 if iscoco==7342
replace offshwalt=59 if iscoco==7343
replace offshwalt=34 if iscoco==7344
replace offshwalt=59 if iscoco==7345
replace offshwalt=75 if iscoco==7346
replace offshwalt=68 if iscoco==7413
replace offshwalt=27 if iscoco==7414
replace offshwalt=55 if iscoco==7415
replace offshwalt=43 if iscoco==7421
replace offshwalt=57 if iscoco==7422
replace offshwalt=57 if iscoco==7423
replace offshwalt=66 if iscoco==7424
replace offshwalt=75 if iscoco==7431
replace offshwalt=75 if iscoco==7432
replace offshwalt=75 if iscoco==7433
replace offshwalt=73 if iscoco==7434
replace offshwalt=75 if iscoco==7435
replace offshwalt=75 if iscoco==7436
replace offshwalt=57 if iscoco==7437
replace offshwalt=75 if iscoco==7441
replace offshwalt=72 if iscoco==7442
replace offshwalt=36 if iscoco==8111
replace offshwalt=36 if iscoco==8112
replace offshwalt=36 if iscoco==8113
replace offshwalt=59 if iscoco==8121
replace offshwalt=68 if iscoco==8122
replace offshwalt=70 if iscoco==8123
replace offshwalt=68 if iscoco==8124
replace offshwalt=69 if iscoco==8131
replace offshwalt=68 if iscoco==8139
replace offshwalt=57 if iscoco==8141
replace offshwalt=68 if iscoco==8142
replace offshwalt=68 if iscoco==8143
replace offshwalt=68 if iscoco==8151
replace offshwalt=70 if iscoco==8152
replace offshwalt=68 if iscoco==8153
replace offshwalt=68 if iscoco==8154
replace offshwalt=29 if iscoco==8155
replace offshwalt=68 if iscoco==8159
replace offshwalt=42 if iscoco==8161
replace offshwalt=55 if iscoco==8162
replace offshwalt=29 if iscoco==8163
replace offshwalt=64 if iscoco==8171
replace offshwalt=68 if iscoco==8172
replace offshwalt=68 if iscoco==8211
replace offshwalt=68 if iscoco==8212
replace offshwalt=68 if iscoco==8221
replace offshwalt=35 if iscoco==8222
replace offshwalt=68 if iscoco==8223
replace offshwalt=41 if iscoco==8224
replace offshwalt=29 if iscoco==8229
replace offshwalt=69 if iscoco==8231
replace offshwalt=68 if iscoco==8232
replace offshwalt=57 if iscoco==8240
replace offshwalt=58 if iscoco==8251
replace offshwalt=59 if iscoco==8252
replace offshwalt=68 if iscoco==8253
replace offshwalt=75 if iscoco==8261
replace offshwalt=75 if iscoco==8262
replace offshwalt=75 if iscoco==8263
replace offshwalt=75 if iscoco==8264
replace offshwalt=75 if iscoco==8265
replace offshwalt=75 if iscoco==8266
replace offshwalt=75 if iscoco==8269
replace offshwalt=27 if iscoco==8271
replace offshwalt=68 if iscoco==8272
replace offshwalt=68 if iscoco==8273
replace offshwalt=30 if iscoco==8274
replace offshwalt=31 if iscoco==8275
replace offshwalt=68 if iscoco==8276
replace offshwalt=27 if iscoco==8277
replace offshwalt=68 if iscoco==8278
replace offshwalt=66 if iscoco==8281
replace offshwalt=66 if iscoco==8282
replace offshwalt=66 if iscoco==8283
replace offshwalt=68 if iscoco==8284
replace offshwalt=57 if iscoco==8285
replace offshwalt=64 if iscoco==8286
replace offshwalt=68 if iscoco==8290
replace offshwalt=34 if iscoco==8340
replace offshwalt=95 if iscoco==9113
replace offshwalt=64 if iscoco==9321
replace offshwalt=29 if iscoco==9333
replace offshwalt=55 if iscoco==1227
replace offshwalt=89 if iscoco==2121
replace offshwalt=100 if iscoco==2132
replace offshwalt=70 if iscoco==2145
replace offshwalt=71 if iscoco==2149
replace offshwalt=81 if iscoco==2211
replace offshwalt=46 if iscoco==2412
replace offshwalt=50 if iscoco==2419
replace offshwalt=33 if iscoco==2432
replace offshwalt=89 if iscoco==2441
replace offshwalt=48 if iscoco==2455
replace offshwalt=72 if iscoco==3115
replace offshwalt=54 if iscoco==3119
replace offshwalt=46 if iscoco==3133
replace offshwalt=34 if iscoco==3139
replace offshwalt=34 if iscoco==3224
replace offshwalt=51 if iscoco==3411
replace offshwalt=25 if iscoco==3415
replace offshwalt=50 if iscoco==3417
replace offshwalt=59 if iscoco==3419
replace offshwalt=51 if iscoco==3432
replace offshwalt=54 if iscoco==3439
replace offshwalt=90 if iscoco==3460
replace offshwalt=54 if iscoco==4122
replace offshwalt=31 if iscoco==4131
replace offshwalt=67 if iscoco==4132
replace offshwalt=46 if iscoco==4141
replace offshwalt=26 if iscoco==4142
replace offshwalt=54 if iscoco==4222
replace offshwalt=68 if iscoco==7141
replace offshwalt=68 if iscoco==7224
replace offshwalt=65 if iscoco==7241
replace offshwalt=34 if iscoco==7344
replace offshwalt=72 if iscoco==7442
replace offshwalt=68 if iscoco==8122
replace offshwalt=68 if iscoco==8139
replace offshwalt=42 if iscoco==8161
replace offshwalt=68 if iscoco==8211
replace offshwalt=68 if iscoco==8223
replace offshwalt=41 if iscoco==8224
replace offshwalt=57 if iscoco==8240
replace offshwalt=58 if iscoco==8251
replace offshwalt=75 if iscoco==8269
replace offshwalt=66 if iscoco==8281
replace offshwalt=66 if iscoco==8283
replace offshwalt=68 if iscoco==8284
replace offshwalt=68 if iscoco==8290
replace offshwalt=70 if iscoco==9322
}

* Create Ordinal Offshwalt Variable
{
gen offshwalt4=.
replace offshwalt4=4 if offshwalt<.
replace offshwalt4=3 if offshwalt<75
replace offshwalt4=2 if offshwalt<50
replace offshwalt4=1 if offshwalt<25
label var offshwalt4 "offshwalt ordinal (4 categories)"
}

* Binary Offshwalt Variable
gen offshwalt2=offshwalt4>1
replace offshwalt2=. if offshwalt==.
label var offshwalt2 "offshwalt binary: =1 if offshwalt>=25 if offshwalt"
}
*******************************************************************************
*****4. Skill specificity provided Iversen, Cusack and Rehm (2011) ************
*******************************************************************************
{
{
*/ This code is provided by Torben Iversen; Thomas Cusack; Philipp Rehm, 2011: Risks at Work The Demand and Supply Sides of Government Redistribution
{
gen relskillspec=.	
	replace relskillspec=6.168528055	if iscoco2==11
	replace relskillspec=2.840445869	if iscoco2==12
	replace relskillspec=1.612754672	if iscoco2==13
	replace relskillspec=3.94911483		if iscoco2==21
	replace relskillspec=2.875304662	if iscoco2==22
	replace relskillspec=1.298205499	if iscoco2==23
	replace relskillspec=3.38271403		if iscoco2==24
	replace relskillspec=5.999693379	if iscoco2==31
	replace relskillspec=5.599717306	if iscoco2==32
	replace relskillspec=2.435374975	if iscoco2==33
	replace relskillspec=3.582262408	if iscoco2==34
	replace relskillspec=1.803739154	if iscoco2==41
	replace relskillspec=3.899735492	if iscoco2==42
	replace relskillspec=3.020082744	if iscoco2==51
	replace relskillspec=0.787535801	if iscoco2==52
	replace relskillspec=4.674256488	if iscoco2==61
	replace relskillspec=3.91546829		if iscoco2==71
	replace relskillspec=3.842807254	if iscoco2==72
	replace relskillspec=20.41910075	if iscoco2==73
	replace relskillspec=9.446026838	if iscoco2==74
	replace relskillspec=25.06473567	if iscoco2==81
	replace relskillspec=12.27676336	if iscoco2==82
	replace relskillspec=3.672294237	if iscoco2==83
	replace relskillspec=7.39344664		if iscoco2==91
	replace relskillspec=7.384150506	if iscoco2==92
	replace relskillspec=6.460338111	if iscoco2==93
	label var relskillspec "Relative skill specificity , Iversen"
}
	
}

}
*******************************************************************************
* 5. task categories Kurer (2020)
*******************************************************************************
{
rename iscoco iscoco_withoutchange
gen iscoco=iscoco_withoutchange
    
{
*generating 3 task categories
gen task = .
gen isco=iscoco
replace task = 1 if inlist(isco, 2411, 2431, 2441, 3411, 3471)
replace task = 1 if inrange(isco, 2100, 2213)
replace task = 1 if inrange(isco, 2443, 2444)
replace task = 1 if inrange(isco, 2446, 2452)
replace task = 1 if inrange(isco, 3100, 3212)
replace task = 1 if inrange(isco, 3433, 3440)
replace task = 1 if inrange(isco, 3442, 3444)

replace task = 2 if inlist(isco, 2442, 2445, 3226, 3432, 3441)
replace task = 2 if inrange(isco, 1000, 1319)
replace task = 2 if inrange(isco, 2220, 2410)
replace task = 2 if inrange(isco, 2412, 2430)
replace task = 2 if inrange(isco, 2432, 2440)
replace task = 2 if inrange(isco, 2453, 2470)
replace task = 2 if inrange(isco, 3213, 3220)
replace task = 2 if inrange(isco, 3222, 3224)
replace task = 2 if inrange(isco, 3229, 3410)
replace task = 2 if inrange(isco, 3412, 3429)
replace task = 2 if inrange(isco, 3449, 3470)
replace task = 2 if inrange(isco, 3472, 3480)

replace task = 3 if isco==4223
replace task = 3 if inrange(isco, 3430, 3431)
replace task = 3 if inrange(isco, 4000, 4195)
replace task = 3 if inrange(isco, 4210, 4215)

replace task = 4 if inlist(isco, 7124, 8340, 9120, 9133)
replace task = 4 if inrange(isco, 1, 110) /* departing from oesch, including 110 (armed forces). this is the actual intention of 1-100 */
replace task = 4 if inrange(isco, 6100, 7113)
replace task = 4 if inrange(isco, 7210, 8290)
replace task = 4 if inrange(isco, 9000, 9001)
replace task = 4 if inrange(isco, 9150, 9151)
replace task = 4 if inrange(isco, 9153, 9161)
replace task = 4 if inrange(isco, 9200, 9311)

replace task = 5 if inlist(isco, 5122, 5143, 9002, 9162)
replace task = 5 if inrange(isco, 7120, 7123)
replace task = 5 if inrange(isco, 7129, 7143)
replace task = 5 if inrange(isco, 8300, 8334)
replace task = 5 if inrange(isco, 9130, 9132)
replace task = 5 if inrange(isco, 9140, 9142)
replace task = 5 if inrange(isco, 9312, 9313)

replace task = 6 if inlist(isco, 3221, 3225, 4200, 9152)
replace task = 6 if inrange(isco, 3227, 3228)
replace task = 6 if inrange(isco, 4220, 4222)
replace task = 6 if inrange(isco, 5000, 5121)
replace task = 6 if inrange(isco, 5123, 5142)
replace task = 6 if inrange(isco, 5149, 5220)
replace task = 6 if inrange(isco, 9003, 9005)
replace task = 6 if inrange(isco, 9100, 9113)
replace task = 6 if inrange(isco, 9320, 9333) /* departing from oesch, including 9333 (transport labourers, animal vehicles) */

* add isco categories 2000 and 3000
* officially not defined, thus not part of oesch's categories

replace task = 1 if inlist(isco, 2000, 3000)

* task3

gen task3 = .
replace task3 = 1 if task==1 | task==2 // NRM
replace task3 = 2 if task==3 | task==4 // R
replace task3 = 3 if task==5 | task==6 // NRC
tab task3


drop isco iscoco
rename iscoco_withoutchange iscoco

}

}
}

************************************************************************
* Generate a dummy to summarize the risk of automation 
gen task3cog1=(task3==1) // dummy for non routine cognitive work - 3 task approach
replace task3cog1=. if task3==.
gen task3cog2and3=(task3==2 | task3==3) // dummy for  routine cognitive work - 3 task approach
replace task3cog2and3=. if task3==.
}

************************************************************************
********************* D. Switching *************************************
************************************************************************
{
* Here I am looking at the answer in 2014 and then comparing with the answer in 2018
// Note: 2014 and 2018 are the first years that the question about presidential election was incldued. THat is pres12 appeared for the first time in 2014 and pres16 in 2018
gen vot_12_14=pres12 if year==2014
replace vot_12_14=. if pres12~=1 & pres12~=2 & pres12~=3 
by id, sort: egen vot12_14=total(vot_12_14)


// This is my main definition of swithcing which is basically only 1 when change, but 0 in all the other cases
gen switching2 = . 
replace switching2 = 1 if vot12_14==1 & pres16==2 // Democrat to Rep
replace switching2 = 0 if vot12_14==1 & pres16==1 // Democrat to Democrat
replace switching2 = 0 if vot12_14==2 //Republicans in 2012
replace switching2 = 0 if vot12_14==1 & pres16~=2 // Democrat to anything except Rep
replace switching2 = 0 if vot12_14==3 // Other candidate

rename switching2 switching2_broad 

	
// This is a more restricted definition of swithchin
gen switching_estrict = . 
replace switching_estrict = 1 if vot12_14==1 & pres16==2 // Democrat to Rep
replace switching_estrict = 0 if vot12_14==1 & pres16==1 // Democrat to Democrat
    

}    
}
*############################################
* Saving the data
*############################################
{
* Labels
{
lab var  switching2_broad "Switching"
lab var  switching_estrict "Switching"
lab var rti "RTI"
lab var female "Female"
lab var age "Age"
lab var educ "Education"
lab var black "Black"
lab var unemployed "Unemployed"
lab var foreign "Foreign born"
lab var nonrelig "Non-Believer"
lab var offshwalt2 "Offshorability"
lab var offshwalt2 "Offshorability"
lab var relskillspec "Skill-Specificity"
lab var t2 "Task-Tech"
lab var t3 "Task-Inter"
lab var task3cog1 "Non-Routine"
lab var task3cog2and3 "Routine"


	
}	


keep id switching2_broad   rti age  female   foreign black unemployed nonrelig rincome  region  offshwalt2 relskillspec t2 t3 rincome wtssnr year educ switching_estrict task3cog2and3

save "Data\GSS.dta", replace

}
}

```


Loading the data.



```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Alternatively load prepared data
*##########################################

use "Data\GSS.dta", clear	
lab var  switching2_broad "Switching broad"  
lab var  switching_estrict "Switching strict"		 	
```

### Figure 3: The effect of exposure to automation on vote-switching. [US Part] 

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, results='hide', collectcode=TRUE}
// Figure 3: The effect of exposure to automation on vote-switching. [US Part] 		
{		
// Graph style		
grstyle clear		
set scheme s2color		
grstyle init		
grstyle set plain, nogrid		
grstyle color background white		
		
		
logit  switching_estrict  rti female age  foreign   rincome offshwalt2  relskillspec t2 t3 i.region  [pw=wtssnr] if year==2018	
		
margins, atmeans at(rti=(-1.52(0.1)2.24)) 		
}	 
```


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation (RTI - Index)) yti(Predicted Probability of Switching (95% CI)) ti("US") saving("Figure\US.gph", replace)		
// Export the same plot to a PDF file
graph export "Figure\US.pdf", replace
```



![Figure 3: The effect of exposure to automation on vote-switching - US part](Figure\\US.pdf){ width=12cm }





### Table A3: Switching Vote, IV - RTI, US



```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################		
* Analysis		
*##########################################		
// table A3: Switching Vote, IV - RTI, US		

eststo clear
eststo: qui logit switching2_broad rti  female age   foreign educ i.rincome  [pw=wtssnr] if year==2018, robust				
eststo: qui logit switching2_broad rti  female age   foreign educ i.rincome offshwalt2  i.region [pw=wtssnr] if year==2018, robust	
eststo: qui logit switching2_broad rti  female age   foreign educ i.rincome offshwalt2 relskillspec  i.region [pw=wtssnr] if year==2018, robust			
eststo: qui logit switching2_broad rti  female age   foreign educ i.rincome offshwalt2 relskillspec t2 t3 unemployed black nonrelig i.region [pw=wtssnr] if year==2018, robust		

esttab , replace label se title(Switching Vote, IV - RTI \label {tab:rtilong})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" ) indicate( "Income = *.rincome"  "Regional controls = *.region")
		
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\USlong_2.tex", replace label se title(Switching Vote, IV - RTI, US \label {tab:rtilong})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" ) indicate( "Income = *.rincome"  "Regional controls = *.region")

```


```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A3}

% Include the table with custom numbering
\input{Table/USlong_2.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```

### Table A5: Switching Vote (alternative definition), IV - RTI

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
// table A5: Switching Vote (alternative definition), IV - RTI		
{		
eststo clear
eststo: qui logit switching_estrict rti  female age   foreign educ i.rincome  i.region [pw=wtssnr] if year==2018, robust		
eststo: qui logit switching_estrict rti  female age   foreign educ i.rincome offshwalt2  i.region [pw=wtssnr] if year==2018, robust		
eststo: qui logit switching_estrict rti  female age   foreign educ i.rincome offshwalt2 relskillspec  i.region [pw=wtssnr] if year==2018, robust		
eststo: qui logit switching_estrict rti  female age   foreign educ i.rincome offshwalt2 relskillspec t2 t3 unemployed black nonrelig i.region [pw=wtssnr] if year==2018, robust		

esttab , replace label se title(Switching Vote (alternative definition), IV - RTI \label {tab:rtilongstrict})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" )  indicate( "Income = *.rincome"  "Regional controls = *.region")		

		
		
}	 	
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\USStrict_2.tex", replace label se title(Switching Vote (alternative definition), IV - RTI, US \label {tab:rtilongstrict})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" )  indicate( "Income = *.rincome"  "Regional controls = *.region")		

```



```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A5}

% Include the table with custom numbering
\input{Table/USStrict_2.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```

### Table A6: Switching Vote, IV - Routine (dummy), US		


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
// table A6: Switching Vote, IV - Routine (dummy), US		
{		
eststo clear
// Routine Dummy
eststo: qui logit switching2_broad task3cog2and3  female age   foreign educ i.rincome  i.region [pw=wtssnr] if year==2018, robust		
eststo: qui logit switching2_broad task3cog2and3  female age   foreign educ i.rincome offshwalt2  i.region [pw=wtssnr] if year==2018, robust
eststo: qui logit switching2_broad task3cog2and3  female age   foreign educ i.rincome offshwalt2 relskillspec i.region [pw=wtssnr] if year==2018, robust		
		
eststo: qui logit switching2_broad task3cog2and3  female age   foreign educ i.rincome offshwalt2 relskillspec t2 t3 unemployed black nonrelig i.region [pw=wtssnr] if year==2018, robust		

esttab , replace label se title(Switching Vote, IV - Routine (dummy) \label {tab:task3cog2and3})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(task3cog2and3  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" ) indicate( "Income = *.rincome"  "Regional controls = *.region")		

		
		
}		
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\USdummy_2.tex", replace label se title(Switching Vote, IV - Routine (dummy), US \label {tab:task3cog2and3})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(task3cog2and3  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" ) indicate( "Income = *.rincome"  "Regional controls = *.region")		

```

```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A6}

% Include the table with custom numbering
\input{Table/USdummy_2}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```

### Table A1: Descriptive statistic: USA GSS 2016 vs 2012	



```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, eval=TRUE}
*##########################################		
* Descriptive 		
*##########################################		

// table A1: Descriptive statistic: USA GSS 2016 vs 2012		

eststo clear
qui estpost sum switching2_broad   rti age  female   foreign black unemployed nonrelig rincome  region  offshwalt2 relskillspec t2 t3  [w=wtssnr] if year==2018, d		

 *esttab ,  /// ,  ,		
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///	
	nonumber label replace noobs varlabels(switching2_broad "Vote Switching" rti "RTI" age "Age" female "Female" foreign "Foreign born" black "Black" unemployed "Unemployed" nonrelig "Non-Believer" rincome "Income Level" offshwalt2 "Offshorability" relskillspec "Skill-Specificity" t2 "Task-Tech" t3 "Task-Inte") nomtitle 
 
esttab using  "Table\summarystats_US.tex",  /// ,  ,		
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///	
	nonumber label replace noobs varlabels(switching2_broad "Vote Switching" rti "RTI" age "Age" female "Female" foreign "Foreign born" black "Black" unemployed "Unemployed" nonrelig "Non-Believer" rincome "Income Level" offshwalt2 "Offshorability" relskillspec "Skill-Specificity" t2 "Task-Tech" t3 "Task-Inte") nomtitle 	

```



```{r, include=TRUE, echo=FALSE}

cat(readLines("Table/summarystats_US.tex"), sep = "\n")
```


```{=latex}
\renewcommand{\thetable}{A1}

\begin{table}[htbp]
    \centering
    \resizebox{\textwidth}{!}{%
        \input{Table/summarystats_US.tex}
    }
    \caption{Descriptive statistic: USA GSS 2016 vs 2012}
\end{table}

\renewcommand{\thetable}{\arabic{table}}

```

## Germany [1_2_Switching_Germany.do]


* This do-file:
	* Processing of the data
		* Call the Data
		* Define variables
		* Save the data
	* Load prepared data **line 869**
	* Analysis and Descriptives: Export Tables &  Figure

* Input: GSS data
	- `Data\Switching\pgen.dta`
	- `Figure/US.gph`  this will be used to merge with `German.gph` (i.e you should run first `1_1_Switching_US.do`)

Final output:

* Cleaned data: 
	* `Data\SOEP.dta` this data contains the relevant variables for the analysis.

*	Tables:
	* table A4: Switching Vote (Only left) - Germany, IV - RTI [`Table\SDU.tex`]
	* table A7: Switching Vote From Establishment Left and Right to Populist Right, IV - RTI, German [`Table\SOEPlong_2.tex`]
	* table A8: Switching Vote, IV - Routine (dummy), Germany [`Table\SOEPdummy_2.tex`]
	* table A9: Switching Vote (Only from the Right), IV - RTI [`Table\CDU.tex`]

*	Figures:
  * Figure 3: The effect of exposure to automation on vote-switching. [German Part]  [`Figure/Germany.gph`]
  * Figure 3: The effect of exposure to automation on vote-switching. [Merging gph US and German part] [`Figure/Graph_US_Germany.pdf`]
		
		
		
	
```{stata, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE}

*##########################################
* Processing of the data (alternatively skip and go to line 788)
*##########################################
{
************************************************************************
**************** A. Calling the data  **********************************
************************************************************************
{

** Set directory
//cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication"


* Calling the data
use "Data\Switching\pgen.dta", clear


keep if syear==2014 | syear==2018 // since elections 2013 vs 2017


}


************************************************************************
********************* B. Demographic  **********************************
************************************************************************
{
* Dummy for born outside the US
gen foreign=.
replace foreign=0 if pgnation==1
replace foreign=. if pgnation<1
replace foreign=1 if pgnation>1 & pgnation~=.

* Dummy for nonreligion
gen nonrelig=1
replace nonrelig=0 if plh0258_h>0 & plh0258_h<12
replace nonrelig=. if plh0258_h<0

gen educ=pgbilzeit
replace educ=. if pgbilzeit<0
replace educ=. if pgbilzeit>40

* Dummy for high level of education
gen high=. 
replace high=1 if educ>15
replace high=0 if educ<=15 & educ>0

* Dummy for female
gen female =. 
replace female = 1 if sex==2
replace female = 0 if sex==1

* Calculating age
destring syear gebjahr, replace
gen age = . 
replace age = syear-gebjahr if gebjahr>0 & gebjahr~=. 
replace age=. if syear<0 | syear==. 
replace age=. if gebjahr<0

* Dummy for unemployed
gen unemployed=. 
replace unemployed=1 if pglfs ==6
replace unemployed=0 if pglfs ~=6 
replace unemployed=. if pglfs <0

gen income=. 
replace income=plb0471_h
replace income=. if plb0471_h<0 | plb0471_h==. 


gen rincome = . 
replace rincome=1 if income<1000
replace rincome=2 if income<2999 & income>999
replace rincome=3 if income<3999 & income>3000
replace rincome=4 if income<4999 & income>4000
replace rincome=5 if income<5999 & income>5000
replace rincome=6 if income<6999 & income>6000
replace rincome=7 if income<7999 & income>7000
replace rincome=8 if income<9999 & income>8000
replace rincome=9 if income<15000 & income>10000
replace rincome=10 if income<20000 & income>14999
replace rincome=11 if income<25000 & income>19999
replace rincome=12 if income>24999 & income~=.

}


************************************************************************
********************* C. Parties  **************************************
************************************************************************
{
* Populist [only Afd]
gen populist=. 
replace populist = 1 if plh0012_h==27 //Alternative fur Deutschland (AfD)
replace populist = 0 if plh0012_h==26 //Piratenpartei
replace populist = 0 if plh0012_h==7 //NDP, Republicans, The Right
replace populist = 0 if plh0012_h~=27 &  plh0012_h~=26 & plh0012_h~=7  
replace populist = . if plh0012_h<1



gen establishment_left2=. 
replace establishment_left2 = 1 if plh0012_h==1 //soc social democratic - spd
replace establishment_left2 = 1 if plh0012_h==4 //[4] FDP	lib liberal
replace establishment_left2 = 0 if plh0012_h~=1 & plh0012_h~=4 
replace establishment_left2 = . if plh0012_h<1

gen establishment_right=. 
replace establishment_right = 1 if plh0012_h==2 
replace establishment_right = 1 if plh0012_h==3 
replace establishment_right = 0 if plh0012_h~=2 & plh0012_h~=3  
replace establishment_right = . if plh0012_h<1

}

************************************************************************
********************* D. Standarizing occupations **********************
************************************************************************
{
gen year=syear
gen iscoco = pgisco88
replace iscoco = . if pgisco88<0
replace iscoco = p_isco88 if pgisco88<0
replace iscoco = . if iscoco<0

foreach var of varlist iscoco  {
gen str_`var'=string(`var')
replace str_`var'="0100" if str_`var'=="100"
}
sum iscoco if str_iscoco=="0100"
foreach x in iscoco {
gen `x'2 = substr(str_`x',1,2) if `x'<9334
replace `x'2 = "." if `x'2==""
}

drop str_*

destring iscoco2, replace

label var iscoco2 "ISCO88 at the 2-digit"

* Labels Iscoco
{
gen iscoco2n="."
replace iscoco2n="Legislators and senior officials" if iscoco2==11
	replace iscoco2n="Corporate managers" if iscoco2==12
	replace iscoco2n="General managers" if iscoco2==13
	replace iscoco2n="Physical, mathematical and engineering science professionals" if iscoco2==21
	replace iscoco2n="Life science and health professionals" if iscoco2==22
	replace iscoco2n="Teaching professionals" if iscoco2==23
	replace iscoco2n="Other professionals" if iscoco2==24
	replace iscoco2n="Physical and engineering science associate professionals" if iscoco2==31
	replace iscoco2n="Life science and health associate professionals" if iscoco2==32
	replace iscoco2n="Teaching associate professionals" if iscoco2==33
	replace iscoco2n="Other associate professionals" if iscoco2==34
	replace iscoco2n="Office clerks" if iscoco2==41
	replace iscoco2n="Customer services clerks" if iscoco2==42
	replace iscoco2n="Personal and protective services workers" if iscoco2==51
	replace iscoco2n="Models, salespersons and demonstrators" if iscoco2==52
	replace iscoco2n="Market-oriented skilled agricultural and fishery workers" if iscoco2==61
	replace iscoco2n="Subsistence agricultural and fishery workers" if iscoco2==62
	replace iscoco2n="Extraction and building trades workers" if iscoco2==71
	replace iscoco2n="Metal, machinery and related trades workers" if iscoco2==72
	replace iscoco2n="Precision, handicraft, printing and related trades workers" if iscoco2==73
	replace iscoco2n="Other craft and related trades workers" if iscoco2==74
	replace iscoco2n="Stationary-plant and related operators" if iscoco2==81
	replace iscoco2n="Machine operators and assemblers" if iscoco2==82
	replace iscoco2n="Drivers and mobile-plant operators" if iscoco2==83
	replace iscoco2n="Sales and services elementary occupations" if iscoco2==91
	replace iscoco2n="Agricultural, fishery and related labourers" if iscoco2==92
	replace iscoco2n="Labourers in mining, construction, manufacturing and transport" if iscoco2==93
	replace iscoco2n="Armed forces" if iscoco2==01
label var iscoco2n "Names of iscoco2 coding"
}
************************************************************************
********************* Exposure to automation ***************************
************************************************************************
{
************************************************************************
* 1. Goos et al.(2014)
************************************************************************
{
** These lines include the routinisation and offshoring indices at the 2-digit level from Goos et al. (2014)

*/ The idea is that for each occupation (at the 2-digit level) one risk is assigned. The weak part of this index is that it treats as the same the last 2-digit of the ISCO.
* In other words, ISCO has 4 digit, but goos et al only consider the first 2 digit, therefore there is no differentiation among occupations that differ in the last 2 digit. 
gen rti=.

{
replace rti=-0.75 if iscoco2==12
replace rti=-0.82 if iscoco2==21
replace rti=-1.00 if iscoco2==22
replace rti=-0.73 if iscoco2==24
replace rti=-1.52 if iscoco2==13
replace rti=-0.40 if iscoco2==31
replace rti=-0.44 if iscoco2==34
replace rti=-0.33 if iscoco2==32

replace rti=0.32 if iscoco2==81
replace rti=0.46 if iscoco2==72
replace rti=-1.50 if iscoco2==83
replace rti=2.24 if iscoco2==41
replace rti=1.59 if iscoco2==73
replace rti=-0.19 if iscoco2==71
replace rti=1.41 if iscoco2==42
replace rti=0.49 if iscoco2==82
replace rti=1.24 if iscoco2==74

replace rti=0.45 if iscoco2==93
replace rti=-0.60 if iscoco2==51
replace rti=0.05 if iscoco2==52
replace rti=0.03 if iscoco2==91
}

label var rti "RTI index"

}
************************************************************************
***** 2. Oesch (2006) and Kitschelt and Rehm (2014) classification *****
************************************************************************
{
*Now is the same, but using Oesch (2006) and Kitschelt and Rehm (2014) classification

*/  Oesch (2006) develops the bases of a new class schema that partly shifts its focus from hierarchical divisions to horizontal cleavages. The idea is  that the middle class is not conceptualized as a unitary grouping and the manual/non-manual divide is not used as a decisive class boundary. 
*/ The emphasis is put on differences in marketable skills and the work logic. 

// What is the logic? 
*/Logic of task structures: t1 organizational (taskorg), t2 technical (tasktech), and t3 interpersonal (taskinter)
*/Logic of authority: a1 professional (authprof), a2 associate professional (authassoc), a3 skilled routine (authskil) a4 unskilled routine (authunsk)
*/Leads to 3*4 groups, in regressions of K&R combined to 4 groups (skilled+unskilled routine all tasks; prof+assoc prof for 3 tasks separately)

* Code from Thewissen and Rueda  (2019)
{
*/t1a1: Higher grade managers
gen t1a1=1 if iscoco>=1000 & iscoco<=1251 | iscoco>=2410 & iscoco<=2419 | inlist(iscoco,2441,2470)
label var t1a1 "Higher grade managers"

*/t1a2: Associate managers
gen t1a2=1 if iscoco>=1252 & iscoco<=1319 | iscoco>=3410 & iscoco<=3449 | inlist(iscoco,3452)
label var t1a2 "Associate managers"

*/t1a3: Skilled office
gen t1a3=1 if iscoco>=4000 & iscoco<=4112 | iscoco>=4114 & iscoco<=4141 | inlist(iscoco,4143) | iscoco>=4190 & iscoco<=4210 | iscoco>=4213 & iscoco<=4221
label var t1a3 "Skilled office"

*/t1a4: Routine office
gen t1a4=1 if inlist(iscoco,4113,4142,4144) | iscoco>=4211 & iscoco<=4212 | iscoco>=4222 & iscoco<=4223
label var t1a4 "Routine office"

*/t2a1: Technical experts
gen t2a1=1 if iscoco>=2100 & iscoco<=2213
label var t2a1 "Technical experts"

*/t2a2: Technicians
gen t2a2=1 if iscoco>=3100 & iscoco<=3213 | inlist(iscoco,3471)
label var t2a2 "Technicians"

*/t2a3: Skilled crafts
gen t2a3=1 if inlist(iscoco,110,8311,8324,8333) | iscoco>=7120 & iscoco<=7142 | iscoco>=7200 & iscoco<=7233 | iscoco>=7240 & iscoco<=7423 | iscoco>=7430 & iscoco<=7520
label var t2a3 "Skilled crafts"

*/t2a4: Routine operatives/agriculture
gen t2a4=1 if inlist(iscoco,7143, 7234, 7424, 8312) | iscoco>=7100 & iscoco<=7113 | iscoco>=7129 & iscoco<=7130 | iscoco>=8000 & iscoco<=8310 | iscoco>=8334 & iscoco<=8400 | iscoco>=9160 & iscoco<=9162 | iscoco>=9300 & iscoco<=9333
replace t2a4=1 if iscoco>=6010 & iscoco<=6210 | iscoco>=8330 & iscoco<=8332 | iscoco>=9200 & iscoco<=9213
label var t2a4 "Routine operatives/agriculture"

*/t3a1: Socio-cultural professionals
gen t3a1=1 if inlist(iscoco, 2359, 2445, 2451, 2460) | iscoco>=2220 & iscoco<=2323 | iscoco>=2350 & iscoco<=2351 |iscoco>=2420 & iscoco<=2440 | iscoco>=2442 & iscoco<=2443
label var t3a1 "Socio-cultural professionals"

*/t3a2: Socio-cultural semi-professionals
gen t3a2=1 if inlist(iscoco,2352, 2444, 3220, 3226) | iscoco>=2330 & iscoco<=2340 | iscoco>=2446 & iscoco<=2450 | iscoco>=2452 & iscoco<=2455 | iscoco>=3222 & iscoco<=3224 | iscoco>=3229 & iscoco<=3232 | iscoco>=3240 & iscoco<=3400 | iscoco>=3450 & iscoco<=3451 | iscoco>=3460 & iscoco<=3470 | iscoco>=3472 & iscoco<=3480
label var t3a2 "Socio-cultural semi-professionals"

*/t3a3: Skilled service
gen t3a3=1 if inlist(iscoco, 3221, 3225, 5122, 5141, 5143, 8323) | iscoco>=3227 & iscoco<=3228 | iscoco>=5110 & iscoco<=5113 | iscoco>=5150 & iscoco<=5163 | iscoco>=5200 & iscoco<=5210
label var t3a3 "Skilled service"

*/t3a4: Routine service
gen t3a4=1 if inlist(iscoco,5142, 5149, 5169) | iscoco>=5120 & iscoco<=5121 | iscoco>=5123 & iscoco<=5130 | iscoco>=5131 & iscoco<=5140 | iscoco>=5220 & iscoco<=5230 | iscoco>=8320 & iscoco<=8322 | iscoco>=9100 & iscoco<=9153
label var t3a4 "Routine service"


forvalues i=1(1)3 {
	gen t`i'=1 if t`i'a1==1 | t`i'a2==1  | t`i'a3==1 | t`i'a4==1
	}
list t1-t3 if t1==t2 & t2==t3 & t1==t3 & t1~=.
replace t1=0 if t2==1 | t3==1
replace t2=0 if t1==1 | t3==1
replace t3=0 if t1==1 | t2==1

label var t1 "Organisational task structure (t1a1, 2, 3, or 4==1)"
label var t2 "Technical task structure (t2a1, 2, 3, or 4==1)"
label var t3 "Interpersonal task structure (t3a1, 2, 3, or 4==1)"

forvalues i=1(1)4 {
	gen a`i'=1 if t1a`i'==1 | t2a`i'==1  | t3a`i'==1
	}
replace a1=0 if a2==1 | a3==1 | a4==1
replace a2=0 if a1==1 | a3==1 | a4==1
replace a3=0 if a1==1 | a2==1 | a4==1
replace a4=0 if a1==1 | a2==1 | a3==1

label var a1 "Professional authority (t1a1, t2a1, or t3a1==1)"
label var a2 "Assoc prof authority (t1a2, t2a2, or t3a2==1)"
label var a3 "Skilled routine authority (t1a3, t2a3, or t3a3==1)"
label var a4 "Unskilled routine authority (t1a4, t2a4, or t3a4==1)"

gen check = a1+a2+a3+a4
sum check
drop check

gen c1=1 if t1a1==1 | t1a2==1
gen c2=1 if t2a1==1 | t2a2==1
gen c3=1 if t3a1==1 | t3a2==1
gen c4=1 if t1a3==1 | t1a4==1 | t2a3==1 | t2a4==1 | t3a3==1 | t3a4==1

replace c1=0 if c2==1 | c3==1 | c4==1
replace c2=0 if c1==1 | c3==1 | c4==1
replace c3=0 if c1==1 | c2==1 | c4==1
replace c4=0 if c1==1 | c2==1 | c3==1

label var c1 "Skilled organisational (t1a1 or t1a2==1)"
label var c2 "Skilled technical (t2a1 or t2a2==1)"
label var c3 "Skilled interpersonal (t3a1 or t3a2==1)"
label var c4 "Unskilled routine (t1a3, t1a4, t2a3, t2a4, t3a3, or t3a4==1)"

gen check = c1+c2+c3+c4
sum check
drop check
}
}
*********************************************************************
***** 3. Offshoring index from Walter (2017), based on Blinder*******
*********************************************************************
{
*/ Stefanie Walter (2017) provides the code for offshorability based on Blinder. It uses ISCO with 4 digits. 

gen offshwalt=.
label var offshwalt "Offshoring Potential (Blinder) from Walter"


*4-digit ISCO-code
 * Coding is based on the classification developed in Blinder, Alan. 2007. "How Many U.S. Jobs Might Be Offshorable." CEPS Working Paper No. 142.
* NOTE: all professions not listed by Blinder are coded as not offshorable (value 0)
* Unlike Goos et al, Walter considers the 4 digits. 
{
replace offshwalt=0 if iscoco<.
replace offshwalt=49 if iscoco==1142
replace offshwalt=55 if iscoco==1222
replace offshwalt=28 if iscoco==1226
replace offshwalt=55 if iscoco==1228
replace offshwalt=83 if iscoco==1231
replace offshwalt=49 if iscoco==1232
replace offshwalt=40 if iscoco==1233
replace offshwalt=53 if iscoco==1234
replace offshwalt=49 if iscoco==1235
replace offshwalt=55 if iscoco==1236
replace offshwalt=55 if iscoco==1237
replace offshwalt=55 if iscoco==1311
replace offshwalt=55 if iscoco==1312
replace offshwalt=55 if iscoco==1313
replace offshwalt=55 if iscoco==1314
replace offshwalt=55 if iscoco==1315
replace offshwalt=48 if iscoco==1316
replace offshwalt=52 if iscoco==1317
replace offshwalt=55 if iscoco==1318
replace offshwalt=55 if iscoco==1319
replace offshwalt=66 if iscoco==2111
replace offshwalt=74 if iscoco==2112
replace offshwalt=66 if iscoco==2113
replace offshwalt=66 if iscoco==2114
replace offshwalt=89 if iscoco==2121
replace offshwalt=96 if iscoco==2122
replace offshwalt=83 if iscoco==2131
replace offshwalt=90 if iscoco==2139
replace offshwalt=25 if iscoco==2141
replace offshwalt=64 if iscoco==2143
replace offshwalt=74 if iscoco==2144
replace offshwalt=72 if iscoco==2146
replace offshwalt=69 if iscoco==2147
replace offshwalt=86 if iscoco==2148
replace offshwalt=71 if iscoco==2149
replace offshwalt=81 if iscoco==2211
replace offshwalt=83 if iscoco==2212
replace offshwalt=72 if iscoco==2411
replace offshwalt=50 if iscoco==2419
replace offshwalt=74 if iscoco==2421
replace offshwalt=67 if iscoco==2444
replace offshwalt=90 if iscoco==2451
replace offshwalt=78 if iscoco==2452
replace offshwalt=25 if iscoco==2453
replace offshwalt=48 if iscoco==2455
replace offshwalt=47 if iscoco==3111
replace offshwalt=47 if iscoco==3113
replace offshwalt=47 if iscoco==3114
replace offshwalt=72 if iscoco==3115
replace offshwalt=47 if iscoco==3116
replace offshwalt=94 if iscoco==3118
replace offshwalt=54 if iscoco==3119
replace offshwalt=75 if iscoco==3121
replace offshwalt=84 if iscoco==3122
replace offshwalt=68 if iscoco==3123
replace offshwalt=36 if iscoco==3131
replace offshwalt=36 if iscoco==3132
replace offshwalt=46 if iscoco==3133
replace offshwalt=34 if iscoco==3139
replace offshwalt=52 if iscoco==3141
replace offshwalt=60 if iscoco==3152
replace offshwalt=55 if iscoco==3211
replace offshwalt=55 if iscoco==3212
replace offshwalt=44 if iscoco==3213
replace offshwalt=32 if iscoco==3228
replace offshwalt=51 if iscoco==3411
replace offshwalt=85 if iscoco==3412
replace offshwalt=50 if iscoco==3414
replace offshwalt=55 if iscoco==3416
replace offshwalt=59 if iscoco==3419
replace offshwalt=51 if iscoco==3421
replace offshwalt=48 if iscoco==3422
replace offshwalt=38 if iscoco==3431
replace offshwalt=51 if iscoco==3432
replace offshwalt=84 if iscoco==3433
replace offshwalt=84 if iscoco==3434
replace offshwalt=54 if iscoco==3439
replace offshwalt=100 if iscoco==3442
replace offshwalt=85 if iscoco==3471
replace offshwalt=30 if iscoco==3472
replace offshwalt=95 if iscoco==4111
replace offshwalt=94 if iscoco==4112
replace offshwalt=100 if iscoco==4113
replace offshwalt=71 if iscoco==4114
replace offshwalt=38 if iscoco==4115
replace offshwalt=84 if iscoco==4121
replace offshwalt=54 if iscoco==4122
replace offshwalt=31 if iscoco==4131
replace offshwalt=67 if iscoco==4132
replace offshwalt=67 if iscoco==4133
replace offshwalt=46 if iscoco==4141
replace offshwalt=26 if iscoco==4142
replace offshwalt=95 if iscoco==4143
replace offshwalt=54 if iscoco==4144
replace offshwalt=54 if iscoco==4190
replace offshwalt=94 if iscoco==4211
replace offshwalt=54 if iscoco==4214
replace offshwalt=65 if iscoco==4215
replace offshwalt=72 if iscoco==4221
replace offshwalt=54 if iscoco==4222
replace offshwalt=50 if iscoco==4223
replace offshwalt=86 if iscoco==5113
replace offshwalt=59 if iscoco==6142
replace offshwalt=36 if iscoco==7111
replace offshwalt=35 if iscoco==7112
replace offshwalt=36 if iscoco==7113
replace offshwalt=65 if iscoco==7211
replace offshwalt=69 if iscoco==7212
replace offshwalt=70 if iscoco==7213
replace offshwalt=70 if iscoco==7222
replace offshwalt=68 if iscoco==7223
replace offshwalt=68 if iscoco==7224
replace offshwalt=26 if iscoco==7311
replace offshwalt=64 if iscoco==7313
replace offshwalt=69 if iscoco==7321
replace offshwalt=69 if iscoco==7322
replace offshwalt=68 if iscoco==7323
replace offshwalt=68 if iscoco==7324
replace offshwalt=60 if iscoco==7331
replace offshwalt=75 if iscoco==7332
replace offshwalt=59 if iscoco==7341
replace offshwalt=59 if iscoco==7342
replace offshwalt=59 if iscoco==7343
replace offshwalt=34 if iscoco==7344
replace offshwalt=59 if iscoco==7345
replace offshwalt=75 if iscoco==7346
replace offshwalt=68 if iscoco==7413
replace offshwalt=27 if iscoco==7414
replace offshwalt=55 if iscoco==7415
replace offshwalt=43 if iscoco==7421
replace offshwalt=57 if iscoco==7422
replace offshwalt=57 if iscoco==7423
replace offshwalt=66 if iscoco==7424
replace offshwalt=75 if iscoco==7431
replace offshwalt=75 if iscoco==7432
replace offshwalt=75 if iscoco==7433
replace offshwalt=73 if iscoco==7434
replace offshwalt=75 if iscoco==7435
replace offshwalt=75 if iscoco==7436
replace offshwalt=57 if iscoco==7437
replace offshwalt=75 if iscoco==7441
replace offshwalt=72 if iscoco==7442
replace offshwalt=36 if iscoco==8111
replace offshwalt=36 if iscoco==8112
replace offshwalt=36 if iscoco==8113
replace offshwalt=59 if iscoco==8121
replace offshwalt=68 if iscoco==8122
replace offshwalt=70 if iscoco==8123
replace offshwalt=68 if iscoco==8124
replace offshwalt=69 if iscoco==8131
replace offshwalt=68 if iscoco==8139
replace offshwalt=57 if iscoco==8141
replace offshwalt=68 if iscoco==8142
replace offshwalt=68 if iscoco==8143
replace offshwalt=68 if iscoco==8151
replace offshwalt=70 if iscoco==8152
replace offshwalt=68 if iscoco==8153
replace offshwalt=68 if iscoco==8154
replace offshwalt=29 if iscoco==8155
replace offshwalt=68 if iscoco==8159
replace offshwalt=42 if iscoco==8161
replace offshwalt=55 if iscoco==8162
replace offshwalt=29 if iscoco==8163
replace offshwalt=64 if iscoco==8171
replace offshwalt=68 if iscoco==8172
replace offshwalt=68 if iscoco==8211
replace offshwalt=68 if iscoco==8212
replace offshwalt=68 if iscoco==8221
replace offshwalt=35 if iscoco==8222
replace offshwalt=68 if iscoco==8223
replace offshwalt=41 if iscoco==8224
replace offshwalt=29 if iscoco==8229
replace offshwalt=69 if iscoco==8231
replace offshwalt=68 if iscoco==8232
replace offshwalt=57 if iscoco==8240
replace offshwalt=58 if iscoco==8251
replace offshwalt=59 if iscoco==8252
replace offshwalt=68 if iscoco==8253
replace offshwalt=75 if iscoco==8261
replace offshwalt=75 if iscoco==8262
replace offshwalt=75 if iscoco==8263
replace offshwalt=75 if iscoco==8264
replace offshwalt=75 if iscoco==8265
replace offshwalt=75 if iscoco==8266
replace offshwalt=75 if iscoco==8269
replace offshwalt=27 if iscoco==8271
replace offshwalt=68 if iscoco==8272
replace offshwalt=68 if iscoco==8273
replace offshwalt=30 if iscoco==8274
replace offshwalt=31 if iscoco==8275
replace offshwalt=68 if iscoco==8276
replace offshwalt=27 if iscoco==8277
replace offshwalt=68 if iscoco==8278
replace offshwalt=66 if iscoco==8281
replace offshwalt=66 if iscoco==8282
replace offshwalt=66 if iscoco==8283
replace offshwalt=68 if iscoco==8284
replace offshwalt=57 if iscoco==8285
replace offshwalt=64 if iscoco==8286
replace offshwalt=68 if iscoco==8290
replace offshwalt=34 if iscoco==8340
replace offshwalt=95 if iscoco==9113
replace offshwalt=64 if iscoco==9321
replace offshwalt=29 if iscoco==9333
replace offshwalt=55 if iscoco==1227
replace offshwalt=89 if iscoco==2121
replace offshwalt=100 if iscoco==2132
replace offshwalt=70 if iscoco==2145
replace offshwalt=71 if iscoco==2149
replace offshwalt=81 if iscoco==2211
replace offshwalt=46 if iscoco==2412
replace offshwalt=50 if iscoco==2419
replace offshwalt=33 if iscoco==2432
replace offshwalt=89 if iscoco==2441
replace offshwalt=48 if iscoco==2455
replace offshwalt=72 if iscoco==3115
replace offshwalt=54 if iscoco==3119
replace offshwalt=46 if iscoco==3133
replace offshwalt=34 if iscoco==3139
replace offshwalt=34 if iscoco==3224
replace offshwalt=51 if iscoco==3411
replace offshwalt=25 if iscoco==3415
replace offshwalt=50 if iscoco==3417
replace offshwalt=59 if iscoco==3419
replace offshwalt=51 if iscoco==3432
replace offshwalt=54 if iscoco==3439
replace offshwalt=90 if iscoco==3460
replace offshwalt=54 if iscoco==4122
replace offshwalt=31 if iscoco==4131
replace offshwalt=67 if iscoco==4132
replace offshwalt=46 if iscoco==4141
replace offshwalt=26 if iscoco==4142
replace offshwalt=54 if iscoco==4222
replace offshwalt=68 if iscoco==7141
replace offshwalt=68 if iscoco==7224
replace offshwalt=65 if iscoco==7241
replace offshwalt=34 if iscoco==7344
replace offshwalt=72 if iscoco==7442
replace offshwalt=68 if iscoco==8122
replace offshwalt=68 if iscoco==8139
replace offshwalt=42 if iscoco==8161
replace offshwalt=68 if iscoco==8211
replace offshwalt=68 if iscoco==8223
replace offshwalt=41 if iscoco==8224
replace offshwalt=57 if iscoco==8240
replace offshwalt=58 if iscoco==8251
replace offshwalt=75 if iscoco==8269
replace offshwalt=66 if iscoco==8281
replace offshwalt=66 if iscoco==8283
replace offshwalt=68 if iscoco==8284
replace offshwalt=68 if iscoco==8290
replace offshwalt=70 if iscoco==9322
}

* Create Ordinal Offshwalt Variable
{
gen offshwalt4=.
replace offshwalt4=4 if offshwalt<.
replace offshwalt4=3 if offshwalt<75
replace offshwalt4=2 if offshwalt<50
replace offshwalt4=1 if offshwalt<25
label var offshwalt4 "offshwalt ordinal (4 categories)"
}

* Binary Offshwalt Variable
gen offshwalt2=offshwalt4>1
replace offshwalt2=. if offshwalt==.
label var offshwalt2 "offshwalt binary: =1 if offshwalt>=25 if offshwalt"
}
*******************************************************************************
*****4. Skill specificity provided Iversen, Cusack and Rehm (2011) ************
*******************************************************************************
{
{
*/ This code is provided by Torben Iversen; Thomas Cusack; Philipp Rehm, 2011: Risks at Work The Demand and Supply Sides of Government Redistribution
*/ See: Raw data\Occupations\Measuring_skill-specificity Iversen, Cusack and Rehm (2011).xlsx column J Relative skill specificity  
{
gen relskillspec=.	
	replace relskillspec=6.168528055	if iscoco2==11
	replace relskillspec=2.840445869	if iscoco2==12
	replace relskillspec=1.612754672	if iscoco2==13
	replace relskillspec=3.94911483		if iscoco2==21
	replace relskillspec=2.875304662	if iscoco2==22
	replace relskillspec=1.298205499	if iscoco2==23
	replace relskillspec=3.38271403		if iscoco2==24
	replace relskillspec=5.999693379	if iscoco2==31
	replace relskillspec=5.599717306	if iscoco2==32
	replace relskillspec=2.435374975	if iscoco2==33
	replace relskillspec=3.582262408	if iscoco2==34
	replace relskillspec=1.803739154	if iscoco2==41
	replace relskillspec=3.899735492	if iscoco2==42
	replace relskillspec=3.020082744	if iscoco2==51
	replace relskillspec=0.787535801	if iscoco2==52
	replace relskillspec=4.674256488	if iscoco2==61
	replace relskillspec=3.91546829		if iscoco2==71
	replace relskillspec=3.842807254	if iscoco2==72
	replace relskillspec=20.41910075	if iscoco2==73
	replace relskillspec=9.446026838	if iscoco2==74
	replace relskillspec=25.06473567	if iscoco2==81
	replace relskillspec=12.27676336	if iscoco2==82
	replace relskillspec=3.672294237	if iscoco2==83
	replace relskillspec=7.39344664		if iscoco2==91
	replace relskillspec=7.384150506	if iscoco2==92
	replace relskillspec=6.460338111	if iscoco2==93
	label var relskillspec "Relative skill specificity , Iversen"
}
	
}

}

*******************************************************************************
* 5 task categories Kurer (2020)
*******************************************************************************
{
rename iscoco iscoco_withoutchange
gen iscoco=iscoco_withoutchange
    
{
*generating 3 task categories
gen task = .
gen isco=iscoco
replace task = 1 if inlist(isco, 2411, 2431, 2441, 3411, 3471)
replace task = 1 if inrange(isco, 2100, 2213)
replace task = 1 if inrange(isco, 2443, 2444)
replace task = 1 if inrange(isco, 2446, 2452)
replace task = 1 if inrange(isco, 3100, 3212)
replace task = 1 if inrange(isco, 3433, 3440)
replace task = 1 if inrange(isco, 3442, 3444)

replace task = 2 if inlist(isco, 2442, 2445, 3226, 3432, 3441)
replace task = 2 if inrange(isco, 1000, 1319)
replace task = 2 if inrange(isco, 2220, 2410)
replace task = 2 if inrange(isco, 2412, 2430)
replace task = 2 if inrange(isco, 2432, 2440)
replace task = 2 if inrange(isco, 2453, 2470)
replace task = 2 if inrange(isco, 3213, 3220)
replace task = 2 if inrange(isco, 3222, 3224)
replace task = 2 if inrange(isco, 3229, 3410)
replace task = 2 if inrange(isco, 3412, 3429)
replace task = 2 if inrange(isco, 3449, 3470)
replace task = 2 if inrange(isco, 3472, 3480)

replace task = 3 if isco==4223
replace task = 3 if inrange(isco, 3430, 3431)
replace task = 3 if inrange(isco, 4000, 4195)
replace task = 3 if inrange(isco, 4210, 4215)

replace task = 4 if inlist(isco, 7124, 8340, 9120, 9133)
replace task = 4 if inrange(isco, 1, 110) /* departing from oesch, including 110 (armed forces). this is the actual intention of 1-100 */
replace task = 4 if inrange(isco, 6100, 7113)
replace task = 4 if inrange(isco, 7210, 8290)
replace task = 4 if inrange(isco, 9000, 9001)
replace task = 4 if inrange(isco, 9150, 9151)
replace task = 4 if inrange(isco, 9153, 9161)
replace task = 4 if inrange(isco, 9200, 9311)

replace task = 5 if inlist(isco, 5122, 5143, 9002, 9162)
replace task = 5 if inrange(isco, 7120, 7123)
replace task = 5 if inrange(isco, 7129, 7143)
replace task = 5 if inrange(isco, 8300, 8334)
replace task = 5 if inrange(isco, 9130, 9132)
replace task = 5 if inrange(isco, 9140, 9142)
replace task = 5 if inrange(isco, 9312, 9313)

replace task = 6 if inlist(isco, 3221, 3225, 4200, 9152)
replace task = 6 if inrange(isco, 3227, 3228)
replace task = 6 if inrange(isco, 4220, 4222)
replace task = 6 if inrange(isco, 5000, 5121)
replace task = 6 if inrange(isco, 5123, 5142)
replace task = 6 if inrange(isco, 5149, 5220)
replace task = 6 if inrange(isco, 9003, 9005)
replace task = 6 if inrange(isco, 9100, 9113)
replace task = 6 if inrange(isco, 9320, 9333) /* departing from oesch, including 9333 (transport labourers, animal vehicles) */

* add isco categories 2000 and 3000
* officially not defined, thus not part of oesch's categories

replace task = 1 if inlist(isco, 2000, 3000)

* task3

gen task3 = .
replace task3 = 1 if task==1 | task==2 // NRM
replace task3 = 2 if task==3 | task==4 // R
replace task3 = 3 if task==5 | task==6 // NRC
tab task3


drop isco iscoco
rename iscoco_withoutchange iscoco

}

}
}
************************************************************************
* Generate a dummy to summarize the risk of automation 
gen task3cog1=(task3==1) // dummy for non routine cognitive work - 3 task approach
replace task3cog1=. if task3==.
gen task3cog2and3=(task3==2 | task3==3) // dummy for  routine cognitive work - 3 task approach
replace task3cog2and3=. if task3==.

}
************************************************************************
********************* E. Switching Vote ********************************
************************************************************************
{
// Generating the years I am interested
{
gen pres12=. 
replace pres12=1 if establishment_left2==1 & syear==2014
replace pres12=2 if establishment_right==1 & syear==2014
replace pres12=3 if populist==1 & syear==2014
replace pres12=4 if plh0012_h>0 & populist~=1 & establishment_right~=1 & establishment_left2~=1 & syear==2014


gen pres16=. 
replace pres16=1 if establishment_left2==1 & syear==2018
replace pres16=2 if establishment_right==1 & syear==2018
replace pres16=3 if populist==1 & syear==2018
replace pres16=4 if plh0012_h>0 & populist~=1 & establishment_right~=1 & establishment_left2~=1 & syear==2018

gen vot_12_14=pres12 if syear==2014
replace vot_12_14=. if pres12~=1 & pres12~=2 & pres12~=3   & pres12~=4
by pid, sort: egen vot12_14=total(vot_12_14)

gen switching2 = . 
replace switching2 = 1 if vot12_14==1 & pres16==3 // left to pop
replace switching2 = 0 if vot12_14==3 & pres16==3 // always pop
replace switching2 = 0 if vot12_14==2 //Republicans in 2012
replace switching2 = 0 if vot12_14==4 //Other
replace switching2 = 0 if vot12_14==3 & pres16~=3 // Populist to anything except Rep

gen switching2_broad = . 
replace switching2_broad = 1 if vot12_14~=3 & pres16==3 // no pop to pop
replace switching2_broad = 0 if vot12_14==3 & pres16==3 // always pop
replace switching2_broad = 0 if pres16~=3 & syear==2018

gen switching2_r = . 
replace switching2_r = 1 if vot12_14==2 & pres16==3 // left to pop
replace switching2_r = 0 if vot12_14==3 & pres16==3 // always pop
replace switching2_r = 0 if vot12_14==1 //Republicans in 2012
replace switching2_r = 0 if vot12_14==4 //Other
replace switching2_r = 0 if vot12_14==3 & pres16~=3 // Populist to anything except Rep



}
}
*############################################
* Saving the data
*############################################
{
// Label
lab var switching2 "Left to Pop Right"
lab var switching2_r "Right to AfD"
lab var switching2_broad "Switching Vote"
lab var rti "RTI Index"
lab var female "Female"
lab var age "Age"
lab var educ "Education"
lab var high "High-Skilled"
lab var unemployed "Unemployed"
lab var foreign "Foreign born"
lab var nonrelig "Non-Believer"
lab var offshwalt2 "Offshorability"
lab var relskillspec "Skill-Specificity"
lab var t2 "Task-Tech"
lab var t3 "Task-Inter"
lab var task3cog1 "Non-Routine"
lab var task3cog2and3 "Routine"
lab var sampreg "Region (West 1 - East 2)"

	
}
keep switching2 switching2_r switching2_broad rti female age educ high unemployed nonrelig offshwalt2 relskillspec t2 t3 task3cog1 task3cog2and3 sampreg pid syear income rincome year  phrf   foreign

save "Data\SOEP.dta", replace

}
```


Loading the data.



```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Alternatively load prepared data
*##########################################

use "Data\SOEP.dta", clear	
	
```



### Figure 3: The effect of exposure to automation on vote-switching. [German Part] 

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, results='hide', collectcode=TRUE}
// Graph style	
grstyle clear	
set scheme s2color	
grstyle init	
grstyle set plain, nogrid	
grstyle color background white	
	
// Figure 3: The effect of exposure to automation on vote-switching. [German part]	
// Margins 	

logit  switching2   rti  female age  foreign   income offshwalt2      [pw=phrf]	if year==2018 
	
margins, atmeans at(rti=(-1.52(0.05)2.24)) 	

	 
```


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
     marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation (RTI - Index)) yti(Predicted Probability of Switching (95% CI)) ti("Germany")  saving("Figure/Germany.gph", replace)	

   	
 graph combine "Figure/US.gph" "Figure/Germany.gph"	
 graph export "Figure/Graph_US_Germany.pdf", as(pdf) replace	
```



![Figure 3: The effect of exposure to automation on vote-switching](Figure\\Graph_US_Germany.pdf){ width=12cm }




### Table A4: Switching Vote (Only left) - Germany, IV - RTI



```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################		
* Analysis		
*##########################################		
// table A4: Switching Vote (Only left) - Germany, IV - RTI	
{	
eststo clear					
eststo: qui logit switching2 rti  female age   foreign high i.rincome   [pw=phrf] 	if year==2018, robust				
eststo: qui logit switching2 rti  female age   foreign high i.rincome offshwalt2  i.sampreg [pw=phrf] 	if year==2018, robust				
eststo: qui logit switching2 rti  female age   foreign high i.rincome offshwalt2 relskillspec  i.sampreg  [pw=phrf] 	if year==2018	, robust			
eststo: qui logit switching2 rti  female age   foreign high i.rincome offshwalt2 relskillspec t2 t3 i.sampreg  [pw=phrf] 	if year==2018	, robust			

esttab, replace label se title(Switching Vote (Only left), IV - RTI \label {tab:left})mti("+Demographic" "+Offshoring" "+Skill"  "All")  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age    high offshwalt2 relskillspec t2 t3) scalars( "N Observations" "r2_p R$^2$" "aic AIC" )indicate( "Income = *income"  "Regional controls = *.sampreg")					

}	

```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\SDU.tex", replace label se title(Switching Vote (Only left), IV - RTI, Germany  \label {tab:left})mti("+Demographic" "+Offshoring" "+Skill"  "All")  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age    high offshwalt2 relskillspec t2 t3) scalars( "N Observations" "r2_p R$^2$" "aic AIC" )indicate( "Income = *income"  "Regional controls = *.sampreg")					

```


```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A4}

% Include the table with custom numbering
\input{Table/SDU.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```

### Table A7: Switching Vote From Establishment Left and Right to Populist Right, IV - RTI, German	

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
// table A7: Switching Vote From Establishment Left and Right to Populist Right, IV - RTI, German	
{	
eststo clear					
eststo: qui logit switching2_broad rti  female age   foreign high i.rincome    [pw=phrf] if year==2018	, robust				
eststo: qui logit switching2_broad rti  female age   foreign high i.rincome offshwalt2  i.sampreg [pw=phrf] if year==2018	, robust				
eststo: qui logit switching2_broad rti  female age   foreign high i.rincome offshwalt2 relskillspec  i.sampreg [pw=phrf] if year==2018		, robust			
eststo: qui logit switching2_broad rti  female age   foreign high i.rincome offshwalt2 relskillspec t2 t3 i.sampreg [pw=phrf] if year==2018		, robust			

esttab, replace label se title(Switching Vote From Establishment Left and Right to Populist Right, IV - RTI, Germany \label {tab:Table1rtilongswnarrow2})mti("+Demographic" "+Offshoring" "+Skill"  "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign high offshwalt2 relskillspec t2 t3) scalars("N Observations" "r2_p R$^2$" "aic AIC") indicate( "Income = *income"  "Regional controls = *.sampreg")					
	
}	
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\SOEPlong_2.tex", replace label se title(Switching Vote From Establishment Left and Right to Populist Right, IV - RTI, Germany \label {tab:Table1rtilongswnarrow2})mti("+Demographic" "+Offshoring" "+Skill"  "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign high offshwalt2 relskillspec t2 t3) scalars("N Observations" "r2_p R$^2$" "aic AIC") indicate( "Income = *income"  "Regional controls = *.sampreg")					

```




```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A7}

% Include the table with custom numbering
\input{Table/SOEPlong_2.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```




### Table A8: Switching Vote, IV - Routine (dummy), Germany		


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
// table A6: Switching Vote, IV - Routine (dummy), US		
{		
eststo clear				
eststo: qui logit switching2_broad task3cog2and3  female age   foreign high i.rincome   [pw=phrf] if year==2018, robust					
					
eststo: qui logit switching2_broad task3cog2and3  female age   foreign high i.rincome offshwalt2  i.sampreg [pw=phrf] if year==2018		, robust			
eststo: qui logit switching2_broad task3cog2and3  female age   foreign high i.rincome offshwalt2 relskillspec i.sampreg [pw=phrf] if year==2018		, robust			
					
eststo: qui logit switching2_broad task3cog2and3  female age   foreign high i.rincome offshwalt2 relskillspec t2 t3 i.sampreg [pw=phrf] if year==2018		, robust			

esttab  , replace label se title(Switching Vote, IV - Routine (dummy), Germany \label {tab:Table1rtilongswnarrowtask3cog2and3})mti("+Demographic" "+Offshoring" "+Skill"  "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(task3cog2and3  female age   foreign high offshwalt2 relskillspec t2 t3) scalars("N Observations" "r2_p R$^2$" "aic AIC") indicate( "Income = *.rincome"  "Regional controls = *.sampreg")				


		
		
}		
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
	esttab  using "Table\SOEPdummy_2.tex", replace label se title(Switching Vote, IV - Routine (dummy), Germany \label {tab:Table1rtilongswnarrowtask3cog2and3})mti("+Demographic" "+Offshoring" "+Skill"  "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(task3cog2and3  female age   foreign high offshwalt2 relskillspec t2 t3) scalars("N Observations" "r2_p R$^2$" "aic AIC") indicate( "Income = *.rincome"  "Regional controls = *.sampreg")				

```

```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A8}

% Include the table with custom numbering
\input{Table/SOEPdummy_2.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```




### Table A9: Switching Vote (Only from the Right), IV - RTI	


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
// table A9: Switching Vote (Only from the Right), IV - RTI	
eststo clear				
	eststo: qui logit switching2_r rti  female age   foreign high  i.rincome offshwalt2  i.sampreg [pw=phrf] if year==2018, robust								
eststo: qui logit switching2_r rti  female age   foreign high  i.rincome offshwalt2  i.sampreg [pw=phrf] if year==2018, robust	
eststo: qui logit switching2_r rti  female age   foreign high  i.rincome offshwalt2 relskillspec  i.sampreg [pw=phrf] if year==2018	, robust		
eststo: qui logit switching2_r rti  female age   foreign high  i.rincome offshwalt2 relskillspec t2 t3 i.sampreg [pw=phrf] if year==2018	, robust				
		
esttab , replace label se title(PSwitching Vote (Only from the Right), IV - RTI \label {tab:CDU})mti("+Demographic" "+Offshoring" "+Skill"  "All")  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign high offshwalt2 relskillspec t2 t3) scalars( "N Observations" "r2_p R$^2$" "aic AIC" )indicate( "Income = *income"  "Regional controls = *.sampreg")					
		

	
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\CDU.tex", replace label se title(PSwitching Vote (Only from the Right), IV - RTI, Germany \label {tab:CDU})mti("+Demographic" "+Offshoring" "+Skill"  "All")  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign high offshwalt2 relskillspec t2 t3) scalars( "N Observations" "r2_p R$^2$" "aic AIC" )indicate( "Income = *income"  "Regional controls = *.sampreg")					

```

```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A9}

% Include the table with custom numbering
\input{Table/CDU.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```

### Table A2: Descriptive statistic: Germany SOEP 2014 vs 2018.




```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, eval=TRUE}
*##########################################		
* Descriptive 		
*##########################################		

// table A2: Descriptive statistic: Germany SOEP 2014 vs 2018.	

eststo clear
qui estpost sum  switching2_broad   rti age income female  foreign unemployed high  offshwalt2 relskillspec t2 t3 sampreg [w=phrf] if switching2_broad~=., d	
	
	
esttab using "Table\summarystats_Germany.tex" ,  /// ,  ,	
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs varlabels(distance_redist "Distance Redistribution" distance_div "Distance Diversity" distance_fixed "Distance Fixed Attributes" PRITM "PR with Trichotomous Multipartism" totseats "Total Number of Seats" number2 "Total Number of Parties" oecdmember "OECD member") nomtitle 


```











```{r, include=TRUE, echo=FALSE}

cat(readLines("Table/summarystats_Germany.tex"), sep = "\n")
```

```{=latex}
\renewcommand{\thetable}{A2}

\begin{table}[htbp]
    \centering
    \resizebox{\textwidth}{!}{%
        \input{Table/summarystats_Germany.tex}
    }
    \caption{Descriptive statistic: USA GSS 2016 vs 2012}
\end{table}

\renewcommand{\thetable}{\arabic{table}}

```


## Additional Context of Switching in Germany [1_3_Switching_Appendix_SpoonKluver.do]


This do-file:

- Creates Table A11 using data from the ESS. 

Input:

- `Data\Appendix_ESS.dta`

Output:

- Table A11: Switching in Western European Countries from Mainstream Left to Outsider Radical Right parties 2002-2018 [`Table\ESS_switching.tex`]


### Table A10: Switching in Germany from mainstream to non-mainstream parties 2002-2009

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Load data
*##########################################

use "Data\SpoonKluever_2019_EJPR_PartyConvergence.dta", clear 


*******************************************************************************
* Preparing variables
*******************************************************************************

* Election years
gen year = 2002 in 1
replace year = 2005 in 2
replace year = 2009 in 3
replace year = 2013 in 4

* Initialize variables
gen SPD = .
gen Liberal = .
gen CDU = .
gen Total_Switching = .

* Election dates and party codes
local dates "22sep2002 18sep2005 27sep2009 22sep2013"
local SPD 41320
local Liberal 41420
local CDU 41521

* Loop over each date
local i = 1
foreach date of local dates {
    * Total Switching for the date
    quietly summarize switch_main if country == 41 & edate == date("`date'", "DMY")
    replace Total_Switching = r(mean) * 100 in `i'

    * SPD
    quietly summarize switch_main if country == 41 & edate == date("`date'", "DMY") & party_last == `SPD'
    replace SPD = r(mean) * 100 in `i'

    * Liberal
    quietly summarize switch_main if country == 41 & edate == date("`date'", "DMY") & party_last == `Liberal'
    replace Liberal = r(mean) * 100 in `i'

    * CDU
    quietly summarize switch_main if country == 41 & edate == date("`date'", "DMY") & party_last == `CDU'
    replace CDU = r(mean) * 100 in `i'

    local i = `i' + 1
}

* Format the variables to display one decimal place
format SPD Liberal CDU Total_Switching %4.1f

// table A10: Switching in Germany from mainstream to non-mainstream parties 2002-2009

* Use tabdisp to display the results in a tabular format
tabdisp year, c(SPD Liberal CDU Total_Switching)
```



## Switching Cross-sectional based on ESS [1_4_Switching_Appendix_ESS.do]

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Load data
*##########################################
use "Data\Appendix_ESS.dta", clear 

```

```{stata, include=FALSE, echo=FALSE, message=FALSE, warning=FALSE, collectcode=TRUE}
*****************************************************************************
* Preparing variables
*****************************************************************************
{
// Generate a binary variable 'west2' to identify whether a country belongs to the specified list of Western countries. 
gen west2=(cou=="BEL" | cou=="DNK" | cou=="FIN" | cou=="FRA" | cou=="DEU" | cou=="GRC" | cou=="IRL" | cou=="NLD" | cou=="PRT" | cou=="ESP" | cou=="SWE" | cou=="GBR" | cou=="NOR" | cou=="CHE" | cou=="AUT" | cou=="ITA" | cou=="POL" )

// Generates a variable called switching comparing parfam and parfam_close. 
	* Parfam indicates the party family of the  party voted in the last election. 
	* parfam_close indicates the party family of the party the subject feel close now. 
	* Switching is defined as voted for another party but now feel close to a nationalist. 
gen switching2=.
replace switching2 =0 if parfam==parfam_close & parfam_close~=. & parfam~=.
replace switching2 =0 if parfam~=parfam_close & parfam_close~=70 
replace switching2 =1 if parfam~=70 & parfam_close==70 

// Generates a variable called switching comparing parfam and parfam_close. The difference with the one before is that this one just look at the left instead of any party.  
gen switching2_leftboth=.
replace switching2_leftboth =0 if parfam==parfam_close & parfam_close~=. & parfam~=.
replace switching2_leftboth =0 if parfam~=parfam_close & parfam_close~=70 
replace switching2_leftboth =1 if (parfam==30 | parfam==20 ) & (parfam_close==70 ) 


// Encode the string variable 'cntry' into a numeric variable 'country2' with value labels
encode cntry, gen(country2)

// Create a new variable 'countr_year' by multiplying 'country2' by 'year'. This creates a unique identifier for each country-year combination
gen countr_year=country2*year

}
```

### Table A11: Switching in Western European Countries from Mainstream Left to Outsider Radical Right parties 2002-2018

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, results='hide', collectcode=TRUE}

// table A11: Switching in Western European Countries from Mainstream Left to Outsider Radical Right parties 2002-2018
{
eststo clear  // Clear any previously stored estimates

// Estimate logistic regression for 'switching2' with various predictors, storing the results. First results are swithing from any party. 
eststo:  logit switching2  meanprobfreyosborne  unemplindiv2 female agea mbtru2 rlgdgr    i.country2 i.year  if west2==1, cluster(countr_year)
eststo:  logit switching2  rti   unemplindiv2  female agea mbtru2 rlgdgr     i.country2 i.year if west2==1, cluster(countr_year)
eststo:  logit switching2  task3cog2and3   unemplindiv2  female agea mbtru2 rlgdgr i.country2 i.year  if west2==1, cluster(countr_year)

// Estimate logistic regression for 'switching2_leftboth' (switching from left parties to nationalist) with various predictors, storing the results
eststo:  logit switching2_leftboth  meanprobfreyosborne  unemplindiv2 female agea mbtru2 rlgdgr     i.country2 i.year   if west2==1, cluster(countr_year)
eststo:  logit switching2_leftboth  rti  unemplindiv2   female agea  mbtru2 rlgdgr    i.country2 i.year  if west2==1 , cluster(countr_year)
eststo:  logit switching2_leftboth  task3cog2and3  unemplindiv2    female agea mbtru2 rlgdgr     i.country2 i.year  if west2==1 , cluster(countr_year)

// Generate an output table with specific variables, saving it in the current results
  	


}
```


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
   esttab 	///
		, keep(meanprobfreyosborne task3cog2and3 rti) replace label se compress nogap star(* 0.1 ** 0.05 *** 0.01) ///
		b(%6.3f) scalars( "N Observations"  )  	///
		indicate("Demographics = *male*" "Socio-econ = *mbtru2*"   "Country FE = *country*" "Year FE = *year", label($\checkmark$ ))	///
		noconstant nonotes nomtitles nodepvars

   esttab using "Table\ESS_switching.tex", ///
		 keep(meanprobfreyosborne task3cog2and3 rti) replace label se compress nogap star(* 0.1 ** 0.05 *** 0.01) ///
		b(%6.3f) scalars( "N Observations"  )  	///
		indicate("Demographics = *male*" "Socio-econ = *mbtru2*"   "Country FE = *country*" "Year FE = *year", label($\checkmark$ ))	///
		noconstant nonotes nomtitles nodepvars

```

```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A11}

% Include the table with custom numbering
\input{Table/ESS_switching.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```

```{r, include=TRUE, echo=FALSE}

cat(readLines("Table/ESS_switching.tex"), sep = "\n")
```

#  Messaging and Targeting Strategies: Candidate Rhetoric and Party Platforms

## Majoritarian Systems: Messaging and Targeting Strategies in the US

### Topic Analysis [2_3_Speech_US_Germany_Appendix_NMF.ipynb]

This file:

- Creates inputs for Table A17: NMF Topic Modeling.
- First it does the NMF for the US, then for Germany.
- Once topic are created the proportion of each one of them is calculated. 


Input:

- `Data/cleaned_data.csv` # Data for the US
- `Data/cleaned_data_G.csv` # Data for Germany


Output:

- Inputs for Table A17: NMF Topic Modeling, 4 clusters, top-10 terms.


### Rallies [2_1_Rally_US.do] 


This do-file:

- Creates table A13 using data collected for rallies, visits, and exposure to automation. 

Input:

- `Data\Rally_Visits_MSA.dta`

Output:

- Table 1: Trump's Campaign Strategy (Close election 5) [`Table\Trump_high_close5.tex`]
- Table A13: Trump's Campaing Strategy (Close election 10) [`Table\Trump_high_close10.tex`]
- Table A14: Trump's Campaing Strategy (Forecasting 2016) [`Table\Trump_high_forec.tex`]
- Table A12: Summary statistics of variables used in this study about Trump's campaign strategies: rallies [`Table\US_rallies_descriptive.tex`]


```{stata, include=TRUE, echo=TRUE, warning=FALSE, message=FALSE, collectcode=TRUE}
*Calling the data
use "Data\Rally_Visits_MSA.dta", clear 
```

```{stata, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE, collectcode=TRUE}
//////////////////////////////////////
* Preparing Variables 
//////////////////////////////////////
{

global statesID AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY




gen rallies_pop=(rallies/Population)*100000 // Relative to the population and by 100K individuals for easier interpretation. This allows for comparisons between areas or groups with different population sizes by standardizing the number of visits according to a common population size. 
gen visits_pop=(visited/Population)*100000
gen anti_pop=(anti/Population)*100000

gen high_pop_pop=(high_pop/Population) // Share of exposed workers 

lab var high_pop "Workers Exposed to Automation"
lab var Pop "Population"
lab var anti_pop "Hate Incidents Per 100K Pop"
lab var high_pop_pop "Workers Exposed to Automation"
lab var close_election "Close Elections"
}


```

#### Table 1: Trump's Campaign Strategy

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
// table 1: Trump's Campaign Strategy
{
gen interaction_pop5=high_pop_pop*close_election5
gen interaction2_pop5=high_pop_pop*close_election5
gen interaction3_pop5=high_pop_pop*anti_pop
gen interaction4_pop5=close_election5*anti_pop

lab var interaction_pop5 "Exposed x Close Elections"
lab var interaction2_pop5 "Exposed x Close Elections"
lab var interaction3_pop5 "Exposed x Hate Incidents"
lab var interaction4_pop5 "Hate Incidents x Close"
lab var close_election5 "Close Elections"


eststo clear

eststo: qui reg rallies_pop high_pop_pop close_election5  anti_pop $statesID , cluster(state_num)
eststo: qui reg rallies_pop high_pop_pop close_election5   interaction2_pop5  anti_pop $statesID ,cluster(state_num)
eststo: qui reg rallies_pop high_pop_pop close_election5   interaction3_pop5  anti_pop $statesID ,cluster(state_num)
eststo: qui reg rallies_pop high_pop_pop close_election5  interaction2_pop5 interaction3_pop5 interaction4_pop5  anti_pop $statesID   ,cluster(state_num)

esttab , replace label se title(Trump's Campaing Strategy \label {TableRallies})  mti("Simple" "Close" "Hate" "All")  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(high* anti_pop close_election* interaction*) scalars( "N Observations" "r2 R$^2$" "aic AIC" )  indicate("FE State = *AK") 



}
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\Trump_high_close5.tex", replace label se title(Trump's Campaing Strategy \label {TableRallies})  mti("Simple" "Close" "Hate" "All")   compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(high* anti_pop close_election* interaction*)  indicate("FE State = *AK" ) scalars( "N Observations" "r2 R$^2$" "aic AIC" )
```


```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{1}

% Include the table with custom numbering
\input{Table/Trump_high_close5.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```

#### Table A13: Trump's Campaing Strategy (Close election 10)

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
// table A13: Trump's Campaing Strategy (Close election 10)
{
gen interaction_pop=high_pop_pop*close_election
gen interaction2_pop=high_pop_pop*close_election
gen interaction3_pop=high_pop_pop*anti_pop
gen interaction4_pop=close_election*anti_pop


lab var interaction_pop "Exposed x Close Elections"
lab var interaction2_pop "Exposed x Close Elections"
lab var interaction3_pop "Exposed x Hate Incidents"
lab var interaction4_pop "Hate Incidents x Close"


eststo clear

eststo: qui reg rallies_pop high_pop_pop close_election  anti_pop $statesID , cluster(state_num)
eststo: qui reg rallies_pop high_pop_pop close_election interaction2_pop  anti_pop $statesID ,cluster(state_num)
eststo: qui reg rallies_pop high_pop_pop close_election   interaction3_pop  anti_pop $statesID ,cluster(state_num)
eststo: qui reg rallies_pop high_pop_pop close_election  interaction2_pop interaction3_pop interaction4_pop  anti_pop $statesID ,cluster(state_num)

esttab , replace label se title(Trump's Campaing Strategy \label {TableRallies})  mti("Simple" "Close" "Hate" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(high* anti_pop close_election interaction*) scalars( "N Observations" "r2 R$^2$" "aic AIC" )  indicate("FE State = *AK") 



}
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\Trump_high_close10.tex", replace label se title(Trump's Campaing Strategy (Close election 10) \label {TableRallies10})  mti("Simple" "Close" "Hate" "All")  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(high* anti_pop close_election* interaction*)  indicate("FE State = *AK" ) scalars( "N Observations" "r2 R$^2$" "aic AIC" )
```


```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A6}

% Include the table with custom numbering
\input{Table/Trump_high_close10.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```



#### Table A14: Trump's Campaing Strategy (Forecasting 2016)

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}

//table A14: Trump's Campaing Strategy (Forecasting 2016)
{
gen interaction_pop_f=high_pop_pop*forescasting2
gen interaction2_pop_f=high_pop_pop*forescasting2
gen interaction3_pop_f=high_pop_pop*anti_pop
gen interaction4_pop_f=forescasting2*anti_pop

lab var interaction_pop_f "Exposed x Close Elections"
lab var interaction2_pop_f "Exposed x Close Elections"
lab var interaction3_pop_f "Exposed x Hate Incidents"
lab var interaction4_pop_f "Hate Incidents x Close"
lab var forescasting2 "Close Elections"

eststo clear

eststo: qui reg rallies_pop high_pop_pop forescasting2  anti_pop $statesID , cluster(state_num)
eststo: qui reg rallies_pop high_pop_pop forescasting2 interaction2_pop_f  anti_pop $statesID ,cluster(state_num)
eststo: qui reg rallies_pop high_pop_pop forescasting2   interaction3_pop_f  anti_pop $statesID ,cluster(state_num)
eststo: qui reg rallies_pop high_pop_pop forescasting2  interaction2_pop_f interaction3_pop_f interaction4_pop_f  anti_pop $statesID ,cluster(state_num)

esttab , replace label se title(Trump's Campaing Strategy \label {TableRallies})  mti("Simple" "Close" "Hate" "All")  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(high* anti_pop forescasting2 interaction*) scalars( "N Observations" "r2 R$^2$" "aic AIC" )   indicate("FE State = *AK") 


}

```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\Trump_high_forec.tex", replace label se title(Trump's Campaing Strategy (Forecasting 2016) \label {TableRallies})  mti("Simple" "Close" "Hate" "All")  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(high* anti_pop forescasting2 interaction*)  indicate("FE State = *AK" ) scalars( "N Observations" "r2 R$^2$" "aic AIC" )
```


```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A14}

% Include the table with custom numbering
\input{Table/Trump_high_forec.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```


#### Table A12: Summary statistics of variables used in this study about Trump's campaign strategies: rallies


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
////////////////////////////////////
* Descriptives
///////////////////////////////////

lab var rallies "\# Rallies per MSA"
lab var visited "Visit MSA (dummy)"

lab var rallies_pop "\# Rallies relative to population"
lab var visits_pop "Visit (dummy) relative to population"

lab var close_election5 "Close election 2012 (5\%)"
lab var forescasting2 "Close election - Forecasting 2016"

lab var close_election "Close election 2012 (10\%)"
lab var anti "\# Hate incident per MSA"
lab var high_pop_pop "Workers Exposed to Automation (relative to pop.)"
lab var high "Workers Exposed to Automation (relative to MSA)"
lab var high_pop "\# Workers Exposed to Automation per MSA"


```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, eval=TRUE}

// table A12: Summary statistics of variables used in this study about Trump's campaign strategies: rallies

eststo clear

qui estpost sum rallies rallies_pop visited visits_pop high_pop high high_pop_pop  anti anti_pop close_election5 forescasting2 close_election, d
esttab , ///
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(2)) max(label(Max) fmt(2)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs
	
esttab using "Table\US_rallies_descriptive.tex", ///
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(2)) max(label(Max) fmt(2)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs

```



```{=latex}
\renewcommand{\thetable}{A1}

\begin{table}[htbp]
    \centering
    \resizebox{\textwidth}{!}{%
        \input{Table/US_rallies_descriptive.tex}
    }
    \caption{Summary statistics of variables used in this study about Trump's campaign strategies: rallies}
\end{table}

\renewcommand{\thetable}{\arabic{table}}

```



```{r, include=TRUE, echo=FALSE}

cat(readLines("Table/US_rallies_descriptive.tex"), sep = "\n")
```


### Campaign Rhetoric  Preparing [2_0_Speech_US_dictionaries.ipynb]


This file:

- Creates dictionaries, and applies them to all Trump speeches in the sample.
- These are inputs for the regression analysis conducted in `2_2_Speech_US.do`



Input:

- `Data/Text/Rallies_MSA.xlsx` It is a xlsx file which contains data on the MSA and an identifier to merge with other files for the analysis. 
- `Data/Text/Presidential...` All txt files. From https://www.presidency.ucsb.edu/
- `Data/Text/Youtube...` All txt files. Additional rallies obtained from Youtube.
  

Output:

- `Data/combined_df.csv`   It is a csv file with the scores per each speech. 



**NOTE: the code for this file can be accessed in the jupyter notebook in the folder `do`.**

### Campaign Rhetoric  Analysis [2_2_Speech_US.do] 


This do-file:

- Creates Table 2 and A15 using data from Trump Speeches. 

Input:

- `Data\Text\combined_df.csv` // This file contains the data from speeches
- `Data\Rally_Visits_MSA.dta` // This file contains information about the MSA (e.g, number of exposed workers)
- Alternatively you can go to line 45 and use prepared data: 
	- `Data\Speech_MSA.dta`

Output:

- Table 2: Trump's Campaign Strategy: Speeches [`Table\Trump_text_IVchanged.tex`]
- Table A15: Trump's Campaing Strategy: Speeches (Total count) [`Table\Trump_text_IVchanged_count.tex`]



```{stata, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE, collectcode=TRUE}
* Merging MSA data with speeches
{
import delimited "Data\Text\combined_df.csv", clear
merge m:m msa_state using "Data\Rally_Visits_MSA.dta"
keep if _merge==3
drop _merge 

// Generating the variables of interest regarding exposure of workers and hate crimes
 
gen high_pop_pop=(high_pop/Population) // Share of exposed workers 
gen anti_pop=(anti/Population)*100000 

keep state  word_count  msa_state  pro_worker_count  culture_count  veryclose10 foreign  month  high_pop_pop   anti_pop 
save "Data\Speech_MSA.dta", replace

}

```




```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Alternatively load prepared data
*##########################################

use "Data\Speech_MSA.dta", clear 
	
```


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}

*******************************************************************************
* Preparing variables
*******************************************************************************
{
encode state, generate(state_num2)  // Encode 'state' as numeric
encode msa_state, generate(msa_num)  // Encode 'msa_state' as numeric

// Calculate word shares
gen pro_w = pro_worker_count / word_count  // Pro-worker word share
gen pro_c = culture_count / word_count  // Pro-culture word share

// Create interaction terms
gen int_exp_close = high_pop_pop * veryclose10  // Interaction: exposure x closeness
gen int_exp_anti = high_pop_pop * anti_pop  // Interaction: exposure x hate incidents

// Label variables
lab var veryclose10 "Close"  
lab var int_exp_close "Exposed x Close"
lab var int_exp_anti "Exposed x Hate"
lab var high_pop_pop "Workers Exp. to Auto."
lab var anti_pop "Hate Inc.x 100K Pop"
}
```


#### Table 2: Trump's Campaign Strategy: Speeches



```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*****************************************
* Regression
*****************************************
  

//table 2: Trump's Campaign Strategy: Speeches
{
preserve  // Preserve the current dataset

keep if msa_num ~= .  // Keep observations with non-missing 'msa_num' (0 observations deleted)

eststo clear  // Clear any previously stored estimates

// DV: Share of pro-worker rhetoric
eststo: qui reg pro_w high_pop_pop veryclose10 i.month foreign anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg pro_w high_pop_pop veryclose10 int_exp_close  i.month foreign anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg pro_w high_pop_pop veryclose10 int_exp_close int_exp_anti i.month foreign anti_pop  i.state_num2,cluster(state_num2)

// DV: Share of pro-culture rhetoric
eststo: qui reg pro_c high_pop_pop veryclose10 i.month foreign anti_pop i.state_num2 ,cluster(state_num2)
eststo: qui reg pro_c high_pop_pop veryclose10 int_exp_close i.month foreign anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg pro_c high_pop_pop veryclose10 int_exp_close int_exp_anti i.month foreign anti_pop  i.state_num2,cluster(state_num2)

// Create regression table

	

restore  // Restore the original dataset

/////
}

  
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
// Save regression table to a .tex file
// Create regression table
esttab , replace label se ///
    title("Trump's Campaign Strategy \label {TableSpeech2}") ///
    compress nogap ///
    star(* 0.1 ** 0.05 *** 0.01) ///
    b(%6.3f) ///
    keep(high_pop* anti* *close* int*) ///
    scalars("N Observations" "r2 R$^2$" "aic AIC") ///
    indicate("FE State = *state*" "Foreign = foreign*" "FE Month = *month") ///
	nomtitle collabels(none) mgroups("Pro-worker Rhetoric (1-3)" "Cultural Rhetoric (4-6)", pattern(1 0 0 1 0 0)  ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span)
	
// Create regression table
esttab using "Table\Trump_text_IVchanged.tex", replace label se ///
    title("Trump's Campaign Strategy \label {TableSpeech2}") ///
    compress nogap ///
    star(* 0.1 ** 0.05 *** 0.01) ///
    b(%6.3f) ///
    keep(high_pop* anti* *close* int*) ///
    scalars("N Observations" "r2 R$^2$" "aic AIC") ///
    indicate("FE State = *state*" "Foreign = foreign*" "FE Month = *month") ///
	nomtitle collabels(none) mgroups("Pro-worker Rhetoric (1-3)" "Cultural Rhetoric (4-6)", pattern(1 0 0 1 0 0)  ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span)
```


```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{2}

% Include the table with custom numbering
\input{Table/Trump_text_IVchanged.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```






#### Table A15: Trump's Campaing Strategy: Speeches (Total count)



```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
// table A15: Trump's Campaing Strategy: Speeches (Total count)
{
preserve  // Preserve the current dataset

keep if msa_num ~= .  // Keep observations with non-missing 'msa_num' (0 observations deleted)

eststo clear  // Clear any previously stored estimates

********* Now number of words counts instead of share of words **************
eststo clear
eststo: qui reg pro_worker_count high_pop_pop  i.month foreign anti_pop i.state_num2 ,cluster(state_num2)
eststo: qui reg pro_worker_count high_pop_pop veryclose10 foreign i.month  anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg pro_worker_count high_pop_pop veryclose10 foreign int_exp_close  i.month  anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg pro_worker_count high_pop_pop veryclose10 foreign int_exp_close int_exp_anti i.month  anti_pop  i.state_num2,cluster(state_num2)

eststo: qui reg culture_count high_pop_pop  i.month foreign anti_pop  i.state_num2 ,cluster(state_num2)
eststo: qui reg culture_count high_pop_pop veryclose10 foreign i.month  anti_pop i.state_num2 ,cluster(state_num2)
eststo: qui reg culture_count high_pop_pop veryclose10 int_exp_close foreign i.month  anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg culture_count high_pop_pop veryclose10 int_exp_close foreign int_exp_anti i.month  anti_pop  i.state_num2,cluster(state_num2)




restore

/////
}

  
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
// Save regression table to a .tex file
esttab , replace label se title(Trump's Campaing Strategy: Speeches (Total count) \label {TableTotal})  nomtitle collabels(none) mgroups("Pro-worker Rhetoric (1-3)" "Cultural Rhetoric (4-6)", pattern(1 0 0 0 1 0 0 0)  ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.2f) keep(high_pop* anti* *close* int*)  scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("FE State = *state*" "Foreign = foreign*" "Fe Month =*month") 

esttab using "Table\Trump_text_IVchanged_count.tex", replace label se title(Trump's Campaing Strategy: Speeches (Total count) \label {TableTotal})  nomtitle collabels(none) mgroups("Pro-worker Rhetoric (1-4)" "Cultural Rhetoric (5-8)", pattern(1 0 0 0 1 0 0 0)  ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span)  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.2f) keep(high_pop* anti* *close* int*)  scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("FE State = *state*" "Foreign = foreign*" "FE Month =*month") 

```


```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A15}

% Include the table with custom numbering
\input{Table/Trump_text_IVchanged_count.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```








##  PRITM Systems: Time-Series-Cross-Sectional Evidence on Messaging


### CMP main analysis [3_2_CMP_PRITM.do] 

This do-file:

	A. Call the Data
	B. Define variables
	C. Export Tables 

Input: **Manifesto Project database**

	- Data\CMP\MPDataset_MPDS2020a_stata14.dta // Data download from https://manifesto-project.wzb.eu/datasets 


Final output:

*	Cleaned data:

	* `Data\CMP_main.dta` this data contains the relevant variables for the analysis with the DV as the polarization over redistribution, and fixed-value positions, estimated as the distance between establishment left and outsider parties.

*	Tables:

	* table 3: PRITM: Partisan Polarization over Redistribution and Fixed Attributes [`Table\TabWithin.tex`]
	* table A19: Partisan Polarization over Redistribution and Fixed Attributes Different Cut-Of [`Table\TabWithin_cutoff.tex`]
	* table A20: Alternative measures of Partisan Polarization over Fixed Attributes between Mainstream Left and Right-Populist [`Table\TabWithin_FValternative.tex`]
	* table A18: Descriptive statistic: PRITM 1970-2019 [`Table\desc_PRITM.tex`]
	

```{stata, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE}
* Processing of the data (alternatively skip and go to line 809)
{
*##########################################
* A. Calling the data
*##########################################
{

use "Data\CMP\MPDataset_MPDS2020a_stata14.dta", replace

}
*############################################
* B. Creating Variables
*############################################
{
* YEAR
gen year = year(edate) 

* Keep countries of interest
keep if countryname=="Australia" |  countryname=="Canada" |  countryname=="Greece" |  countryname=="New Zealand" |  countryname=="Portugal" |  countryname=="Spain" |  countryname=="United Kingdom" |  countryname=="United States" |  countryname=="France" |  countryname=="Norway" | countryname=="Austria" | countryname=="Belgium" | countryname=="Denmark" | countryname=="Estonia" | countryname=="Finland" | countryname=="Hungary" | countryname=="Germany" | countryname=="Iceland"  | countryname=="Ireland"  | countryname=="Italy"  | countryname=="Netherlands"  | countryname=="Norway" | countryname=="Slovakia" | countryname=="Slovenia" | countryname=="Sweden" | countryname=="Switzerland"

* Defines a dummy to identify PRITM countries
gen PRITM=. 
replace PRITM=0 if countryname=="Australia"
replace PRITM=0 if countryname=="Canada"
replace PRITM=0 if countryname=="Greece"
replace PRITM=0 if countryname=="New Zealand"
replace PRITM=0 if countryname=="Portugal"
replace PRITM=0 if countryname=="Spain"
replace PRITM=0 if countryname=="United Kingdom"
replace PRITM=0 if countryname=="United States"
replace PRITM=0 if countryname=="France"

replace PRITM=1 if countryname=="Austria"
replace PRITM=1 if countryname=="Belgium"
replace PRITM=1 if countryname=="Denmark"
replace PRITM=1 if countryname=="Estonia"
replace PRITM=1 if countryname=="Finland"
replace PRITM=1 if countryname=="Germany"
replace PRITM=1 if countryname=="Hungary"
replace PRITM=1 if countryname=="Iceland"
replace PRITM=1 if countryname=="Ireland"
replace PRITM=1 if countryname=="Italy"
replace PRITM=1 if countryname=="Netherlands"
replace PRITM=1 if countryname=="Norway"
replace PRITM=1 if countryname=="Slovenia"
replace PRITM=1 if countryname=="Slovakia"
replace PRITM=1 if countryname=="Sweden"
replace PRITM=1 if countryname=="Switzerland"


* CONTROL VARIABLES - number of parties

gen number = 1
egen number2= sum(number), by(edate)
lab var number2 "Number of parties"

*############################################################
* DEPENDENT VARIABLE PRITM
*############################################################
{
* Obtaining relevant policy variables
{
gen welfare_policy =  ln(per504+0.5) - ln(per505+0.5)

gen national_neg =  (ln(0.5 +per107)-ln(0.5 +per109)) // internationalism narrow

gen fixed = .
replace fixed = (ln(per107+per108+per407+per602+0.5+per602_2)-ln(per109+per110+per406+per601+0.5+per601_2)) if !missing(per602_2) & !missing(per601_2)
replace fixed = (ln(per107+per108+per407+per602+0.5)-ln(per109+per110+per406+per601+0.5)) if missing(per602_2) | missing(per601_2)




gen fixed_broad_policy = .
replace fixed_broad_policy = (ln(per107+per108+per407+per602+0.5 + per602_2+per604+per607)-ln(per109+per110+per406+per601+0.5 + per601_2+per603 +per608)) if !missing(per602_2) & !missing(per601_2)
replace fixed_broad_policy = (ln(per107+per108+per407+per602+0.5 +per604+per607)-ln(per109+per110+per406+per601+0.5 +per603 +per608)) if  missing(per602_2) | missing(per601_2)

gen fixed_eu_policy =  .
replace fixed_eu_policy =  (ln(per108+per602_2+0.5)-ln(per110+per601_2+0.5)) if !missing(per602_2) & !missing(per601_2)
replace fixed_eu_policy =  (ln(per108+0.5)-ln(per110+0.5)) if  missing(per602_2) | missing(per601_2)


gen fixed_nolog1_policy =  .
replace fixed_nolog1_policy=(per109-per107) + (per601 - per602) + (per603 - per604) + (per608 - per607) + (per601_2-per602_2) + (per406-per407)+(per110-per108) if !missing(per602_2) & !missing(per601_2)
replace fixed_nolog1_policy=(per109-per107) + (per601 - per602) + (per603 - per604) + (per608 - per607)  + (per406-per407)+(per110-per108) if missing(per602_2) | missing(per601_2) 
}
* DEFINE PARTY FAMILY GROUPS
{
**  Mainstream left: - soc social democratic and socialist or other left
gen m_left = . 
replace m_left = 1 if parfam== 30 | parfam== 20
replace m_left = 0 if parfam ~= 30 & parfam ~=20 & parfam ~=. 

egen m_left_participation= max(m_left), by(edate)


**  Mainstream left: only soc social democratic
gen m_left_restrict = . 
replace m_left_restrict = 1 if parfam== 30
replace m_left_restrict = 0 if parfam ~= 30 & parfam ~=. 

** Other left: Ecologista and socialist
gen o_left = . 
replace o_left = 1 if  parfam== 10  // here I am also including socialist or other left
replace o_left = 0 if parfam ~= 10  & parfam ~=. 

egen o_left_participation= max(o_left), by(edate)

** Mainstream right: lib liberal, Christian Democrat, Conservatives
gen m_right = . 
replace m_right = 1 if parfam == 40 | parfam == 50 | parfam == 60
replace m_right = 0 if parfam ~= 40 & parfam ~= 50 & parfam ~= 60 & parfam ~=. 

egen m_right_participation= max(m_right), by(edate)

** Other right: agriculture
gen o_right = . 
replace o_right = 1 if parfam == 80
replace o_right = 0 if parfam ~= 80 & parfam ~=. 

egen o_right_participation= max(o_right), by(edate)

** Radical right:  nat nationalist 
gen rad_right = . 
replace rad_right = 1 if parfam == 70 
replace rad_right = 0 if parfam ~= 70 & parfam ~=. 

egen rad_right_participation= max(rad_right), by(edate)


** Other parties: special issues and Ethnic and regional parties
gen o_parties = . 
replace o_parties = 1 if parfam == 90 | parfam == 95 
replace o_parties = 0 if parfam ~= 90 & parfam ~= 95  & parfam ~=. 

egen o_parties_participation= max(o_parties), by(edate)
}
** DISTANCE BY PARTY FAMILY
{

gen welfare_m_left = m_left*welfare_policy if m_left==1
gen welfare_rad_right = rad_right*welfare_policy if rad_right==1
gen welfare_m_right = m_right*welfare_policy if m_right==1

gen welfare_o_left = o_left*welfare_policy if o_left==1
gen welfare_o_right = o_right*welfare_policy if o_right==1
gen welfare_o_parties = o_parties*welfare_policy if o_parties==1

gen national_m_left = m_left*national_neg if m_left==1
gen national_rad_right = rad_right*national_neg if rad_right==1
gen national_m_right = m_right*national_neg if m_right==1

gen fixed_m_left = m_left*fixed if m_left==1
gen fixed_rad_right = rad_right*fixed if rad_right==1
gen fixed_m_right = m_right*fixed if m_right==1

gen fixed_o_left = o_left*fixed if o_left==1
gen fixed_o_right = o_right*fixed if o_right==1
gen fixed_o_parties = o_parties*fixed if o_parties==1


gen fixed_broad_m_left = m_left*fixed_broad_policy if m_left==1
gen fixed_broad_rad_right = rad_right*fixed_broad_policy if rad_right==1
gen fixed_broad_m_right = m_right*fixed_broad_policy if m_right==1

gen fixed_eu_m_left = m_left*fixed_eu_policy if m_left==1
gen fixed_eu_rad_right = rad_right*fixed_eu_policy if rad_right==1
gen fixed_eu_m_right = m_right*fixed_eu_policy if m_right==1

gen fixed_nolog1_m_left = m_left*fixed_nolog1_policy if m_left==1
gen fixed_nolog1_rad_right = rad_right*fixed_nolog1_policy if rad_right==1
gen fixed_nolog1_m_right = m_right*fixed_nolog1_policy if m_right==1


sort countryname edate

replace welfare_m_left = 0 if welfare_m_left ==.
replace welfare_rad_right = 0 if welfare_rad_right ==.
replace welfare_m_right = 0 if welfare_m_right ==.

replace national_m_left = 0 if national_m_left ==.
replace national_rad_right = 0 if national_rad_right ==.
replace national_m_right = 0 if national_m_right ==.


replace  fixed_m_left = 0 if  fixed_m_left ==.
replace  fixed_rad_right = 0 if  fixed_rad_right ==.
replace  fixed_m_right = 0 if  fixed_m_right ==.



replace fixed_broad_m_left = 0 if fixed_broad_m_left ==.
replace fixed_broad_rad_right = 0 if fixed_broad_rad_right ==.
replace fixed_broad_m_right = 0 if fixed_broad_m_right ==.

replace fixed_eu_m_left = 0 if fixed_eu_m_left ==.
replace fixed_eu_rad_right = 0 if fixed_eu_rad_right ==.
replace fixed_eu_m_right = 0 if fixed_eu_m_right ==.



replace fixed_nolog1_m_left = 0 if fixed_nolog1_m_left ==.
replace fixed_nolog1_rad_right = 0 if fixed_nolog1_rad_right ==.
replace fixed_nolog1_m_right = 0 if fixed_nolog1_m_right ==.


}	
* Collapsing the data 
{
collapse (sum) fixed_m_left fixed_m_right  fixed_rad_right   welfare_m_left welfare_rad_right welfare_m_right national_m_left national_rad_right national_m_right   fixed_broad_m_left fixed_broad_rad_right fixed_broad_m_right fixed_eu_m_left fixed_eu_rad_right fixed_eu_m_right fixed_nolog1_m_left fixed_nolog1_rad_right fixed_nolog1_m_right  (first) year  PRITM  totseats number2 oecdmember date *_participation , by(edate countryname)
}
* Final prep: absolute distance
{
gen distance_redist = abs(welfare_m_left-welfare_m_right)
replace distance_redist = abs(welfare_m_left-welfare_rad_right) if PRITM==1

gen distance_nat = abs(national_m_left-national_m_right)
replace distance_nat = abs(national_m_left-national_rad_right) if PRITM==1


gen distance_fixed = abs(fixed_m_left-fixed_m_right)
replace distance_fixed = abs(fixed_m_left-fixed_rad_right) if PRITM==1



gen distance_fixed_all = abs(fixed_broad_m_left-fixed_broad_m_right)
replace distance_fixed_all = abs(fixed_broad_m_left-fixed_broad_rad_right) if PRITM==1

gen distance_fixed_eu= abs(fixed_eu_m_left-fixed_eu_m_right)
replace distance_fixed_eu = abs(fixed_eu_m_left-fixed_eu_rad_right) if PRITM==1


gen distance_fixed_nolog= abs(fixed_nolog1_m_left-fixed_nolog1_m_right)
replace distance_fixed_nolog = abs(fixed_nolog1_m_left-fixed_nolog1_rad_right) if PRITM==1

}


}
// Final Prep of the data
{
sort countryname edate

// Define dummy for the period pre or post 1994 when LMP was high
gen shock=.
replace shock =0 if year > 1969 & year < 1995
replace shock =1 if  year > 1994

egen country_number = group(countryname)

sort country_number edate
bysort country_number: gen election_order=_n
sort countryname  election_order

xtset country_number election_order
	
}
*############################################################
* IFR - Alternative to the shock IV which is just  a dummy 
*############################################################
{
// This includes the data provided by IFR by year and country about the number of industrial robots. Data available from 2004 to 2019	
gen IFR=. 
*2004
{
replace IFR =0 if countryname =="Canada" & year ==2004
replace IFR =123663 if countryname =="Canada" & year ==2004
replace IFR =4170 if countryname =="Canada" & year ==2004
replace IFR =0 if countryname =="Canada" & year ==2004
replace IFR =391 if countryname =="Canada" & year ==2004
replace IFR =285 if countryname =="Canada" & year ==2004
replace IFR =458 if countryname =="Canada" & year ==2004
replace IFR =2 if countryname =="Canada" & year ==2004
replace IFR =3907 if countryname =="Canada" & year ==2004
replace IFR =5987 if countryname =="Canada" & year ==2004
replace IFR =120544 if countryname =="Canada" & year ==2004
replace IFR =21893 if countryname =="Canada" & year ==2004
replace IFR =28133 if countryname =="Canada" & year ==2004
replace IFR =53244 if countryname =="Canada" & year ==2004
replace IFR =2762 if countryname =="Canada" & year ==2004
replace IFR =1488 if countryname =="Canada" & year ==2004
replace IFR =3540 if countryname =="Canada" & year ==2004
replace IFR =14176 if countryname =="Canada" & year ==2004
replace IFR =2342 if countryname =="Canada" & year ==2004
replace IFR =3712 if countryname =="Canada" & year ==2004
replace IFR =724 if countryname =="Canada" & year ==2004
replace IFR =7341 if countryname =="Canada" & year ==2004
replace IFR =63 if countryname =="Canada" & year ==2004
replace IFR =6 if countryname =="Canada" & year ==2004
replace IFR =31 if countryname =="Canada" & year ==2004
}
*2005
{
replace IFR =0 if countryname =="Canada" & year ==2005
replace IFR =139984 if countryname =="United States" & year ==2005
replace IFR =4915 if countryname =="Australia" & year ==2005
replace IFR =23 if countryname =="New Zealand" & year ==2005
replace IFR =460 if countryname =="Slovenia" & year ==2005
replace IFR =458 if countryname =="Hungary" & year ==2005
replace IFR =576 if countryname =="Slovakia" & year ==2005
replace IFR =4 if countryname =="Estonia" & year ==2005
replace IFR =4148 if countryname =="Austria" & year ==2005
replace IFR =6124 if countryname =="Belgium" & year ==2005
replace IFR =126294 if countryname =="Germany" & year ==2005
replace IFR =24141 if countryname =="Spain" & year ==2005
replace IFR =30236 if countryname =="France" & year ==2005
replace IFR =56198 if countryname =="Italy" & year ==2005
replace IFR =3238 if countryname =="Netherlands" & year ==2005
replace IFR =1542 if countryname =="Portugal" & year ==2005
replace IFR =3732 if countryname =="Switzerland" & year ==2005
replace IFR =14948 if countryname =="United Kingdom" & year ==2005
replace IFR =2661 if countryname =="Denmark" & year ==2005
replace IFR =4159 if countryname =="Finland" & year ==2005
replace IFR =811 if countryname =="Norway" & year ==2005
replace IFR =8028 if countryname =="Sweden" & year ==2005
replace IFR =73 if countryname =="Greece" & year ==2005
replace IFR =7 if countryname =="Iceland" & year ==2005
replace IFR =121 if countryname =="Ireland" & year ==2005
	
}
*2006
{

replace IFR =0 if countryname =="Canada" & year ==2006
replace IFR =150725 if countryname =="United States" & year ==2006
replace IFR =5478 if countryname =="Australia" & year ==2006
replace IFR =76 if countryname =="New Zealand" & year ==2006
replace IFR =560 if countryname =="Slovenia" & year ==2006
replace IFR =592 if countryname =="Hungary" & year ==2006
replace IFR =596 if countryname =="Slovakia" & year ==2006
replace IFR =4 if countryname =="Estonia" & year ==2006
replace IFR =4382 if countryname =="Austria" & year ==2006
replace IFR =6331 if countryname =="Belgium" & year ==2006
replace IFR =132594 if countryname =="Germany" & year ==2006
replace IFR =26008 if countryname =="Spain" & year ==2006
replace IFR =32110 if countryname =="France" & year ==2006
replace IFR =58898 if countryname =="Italy" & year ==2006
replace IFR =3797 if countryname =="Netherlands" & year ==2006
replace IFR =1710 if countryname =="Portugal" & year ==2006
replace IFR =3940 if countryname =="Switzerland" & year ==2006
replace IFR =15082 if countryname =="United Kingdom" & year ==2006
replace IFR =3013 if countryname =="Denmark" & year ==2006
replace IFR =4349 if countryname =="Finland" & year ==2006
replace IFR =960 if countryname =="Norway" & year ==2006
replace IFR =8245 if countryname =="Sweden" & year ==2006
replace IFR =90 if countryname =="Greece" & year ==2006
replace IFR =13 if countryname =="Iceland" & year ==2006
replace IFR =191 if countryname =="Ireland" & year ==2006
	
}

*2007
{
	
replace IFR =0 if countryname =="Canada" & year ==2007
replace IFR =160632 if countryname =="United States" & year ==2007
replace IFR =5998 if countryname =="Australia" & year ==2007
replace IFR =129 if countryname =="New Zealand" & year ==2007
replace IFR =709 if countryname =="Slovenia" & year ==2007
replace IFR =772 if countryname =="Hungary" & year ==2007
replace IFR =677 if countryname =="Slovakia" & year ==2007
replace IFR =6 if countryname =="Estonia" & year ==2007
replace IFR =4761 if countryname =="Austria" & year ==2007
replace IFR =6301 if countryname =="Belgium" & year ==2007
replace IFR =139980 if countryname =="Germany" & year ==2007
replace IFR =27473 if countryname =="Spain" & year ==2007
replace IFR =33462 if countryname =="France" & year ==2007
replace IFR =61589 if countryname =="Italy" & year ==2007
replace IFR =4347 if countryname =="Netherlands" & year ==2007
replace IFR =1892 if countryname =="Portugal" & year ==2007
replace IFR =4215 if countryname =="Switzerland" & year ==2007
replace IFR =15340 if countryname =="United Kingdom" & year ==2007
replace IFR =3514 if countryname =="Denmark" & year ==2007
replace IFR =4495 if countryname =="Finland" & year ==2007
replace IFR =1012 if countryname =="Norway" & year ==2007
replace IFR =8830 if countryname =="Sweden" & year ==2007
replace IFR =144 if countryname =="Greece" & year ==2007
replace IFR =13 if countryname =="Iceland" & year ==2007
replace IFR =288 if countryname =="Ireland" & year ==2007

}
*2008
{
	
replace IFR =0 if countryname =="Canada" & year ==2008
replace IFR =168489 if countryname =="United States" & year ==2008
replace IFR =6529 if countryname =="Australia" & year ==2008
replace IFR =185 if countryname =="New Zealand" & year ==2008
replace IFR =852 if countryname =="Slovenia" & year ==2008
replace IFR =1014 if countryname =="Hungary" & year ==2008
replace IFR =860 if countryname =="Slovakia" & year ==2008
replace IFR =7 if countryname =="Estonia" & year ==2008
replace IFR =5122 if countryname =="Austria" & year ==2008
replace IFR =6276 if countryname =="Belgium" & year ==2008
replace IFR =144643 if countryname =="Germany" & year ==2008
replace IFR =28636 if countryname =="Spain" & year ==2008
replace IFR =34370 if countryname =="France" & year ==2008
replace IFR =63051 if countryname =="Italy" & year ==2008
replace IFR =4848 if countryname =="Netherlands" & year ==2008
replace IFR =1990 if countryname =="Portugal" & year ==2008
replace IFR =4431 if countryname =="Switzerland" & year ==2008
replace IFR =15080 if countryname =="United Kingdom" & year ==2008
replace IFR =3891 if countryname =="Denmark" & year ==2008
replace IFR =4663 if countryname =="Finland" & year ==2008
replace IFR =1031 if countryname =="Norway" & year ==2008
replace IFR =9426 if countryname =="Sweden" & year ==2008
replace IFR =203 if countryname =="Greece" & year ==2008
replace IFR =13 if countryname =="Iceland" & year ==2008
replace IFR =340 if countryname =="Ireland" & year ==2008

}
*2009
{
	
replace IFR =0 if countryname =="Canada" & year ==2009
replace IFR =166183 if countryname =="United States" & year ==2009
replace IFR =6402 if countryname =="Australia" & year ==2009
replace IFR =295 if countryname =="New Zealand" & year ==2009
replace IFR =903 if countryname =="Slovenia" & year ==2009
replace IFR =1207 if countryname =="Hungary" & year ==2009
replace IFR =1068 if countryname =="Slovakia" & year ==2009
replace IFR =32 if countryname =="Estonia" & year ==2009
replace IFR =5398 if countryname =="Austria" & year ==2009
replace IFR =6448 if countryname =="Belgium" & year ==2009
replace IFR =144133 if countryname =="Germany" & year ==2009
replace IFR =28781 if countryname =="Spain" & year ==2009
replace IFR =34099 if countryname =="France" & year ==2009
replace IFR =62242 if countryname =="Italy" & year ==2009
replace IFR =5230 if countryname =="Netherlands" & year ==2009
replace IFR =2144 if countryname =="Portugal" & year ==2009
replace IFR =4377 if countryname =="Switzerland" & year ==2009
replace IFR =13923 if countryname =="United Kingdom" & year ==2009
replace IFR =4076 if countryname =="Denmark" & year ==2009
replace IFR =4719 if countryname =="Finland" & year ==2009
replace IFR =1025 if countryname =="Norway" & year ==2009
replace IFR =9396 if countryname =="Sweden" & year ==2009
replace IFR =242 if countryname =="Greece" & year ==2009
replace IFR =13 if countryname =="Iceland" & year ==2009
replace IFR =370 if countryname =="Ireland" & year ==2009

}
* 2010
{
	
replace IFR =0 if countryname =="Canada" & year ==2010
replace IFR =173174 if countryname =="United States" & year ==2010
replace IFR =6679 if countryname =="Australia" & year ==2010
replace IFR =387 if countryname =="New Zealand" & year ==2010
replace IFR =1032 if countryname =="Slovenia" & year ==2010
replace IFR =1406 if countryname =="Hungary" & year ==2010
replace IFR =1870 if countryname =="Slovakia" & year ==2010
replace IFR =36 if countryname =="Estonia" & year ==2010
replace IFR =5749 if countryname =="Austria" & year ==2010
replace IFR =6251 if countryname =="Belgium" & year ==2010
replace IFR =148256 if countryname =="Germany" & year ==2010
replace IFR =28868 if countryname =="Spain" & year ==2010
replace IFR =34495 if countryname =="France" & year ==2010
replace IFR =62378 if countryname =="Italy" & year ==2010
replace IFR =5438 if countryname =="Netherlands" & year ==2010
replace IFR =2280 if countryname =="Portugal" & year ==2010
replace IFR =4417 if countryname =="Switzerland" & year ==2010
replace IFR =13519 if countryname =="United Kingdom" & year ==2010
replace IFR =4234 if countryname =="Denmark" & year ==2010
replace IFR =4611 if countryname =="Finland" & year ==2010
replace IFR =1012 if countryname =="Norway" & year ==2010
replace IFR =9387 if countryname =="Sweden" & year ==2010
replace IFR =286 if countryname =="Greece" & year ==2010
replace IFR =13 if countryname =="Iceland" & year ==2010
replace IFR =411 if countryname =="Ireland" & year ==2010

}
*2011
{
	
replace IFR =1848 if countryname =="Canada" & year ==2011
replace IFR =180893 if countryname =="United States" & year ==2011
replace IFR =7189 if countryname =="Australia" & year ==2011
replace IFR =510 if countryname =="New Zealand" & year ==2011
replace IFR =1194 if countryname =="Slovenia" & year ==2011
replace IFR =2347 if countryname =="Hungary" & year ==2011
replace IFR =2210 if countryname =="Slovakia" & year ==2011
replace IFR =47 if countryname =="Estonia" & year ==2011
replace IFR =6104 if countryname =="Austria" & year ==2011
replace IFR =6243 if countryname =="Belgium" & year ==2011
replace IFR =157241 if countryname =="Germany" & year ==2011
replace IFR =29847 if countryname =="Spain" & year ==2011
replace IFR =34461 if countryname =="France" & year ==2011
replace IFR =62245 if countryname =="Italy" & year ==2011
replace IFR =6108 if countryname =="Netherlands" & year ==2011
replace IFR =2372 if countryname =="Portugal" & year ==2011
replace IFR =4717 if countryname =="Switzerland" & year ==2011
replace IFR =13641 if countryname =="United Kingdom" & year ==2011
replace IFR =4417 if countryname =="Denmark" & year ==2011
replace IFR =4473 if countryname =="Finland" & year ==2011
replace IFR =1025 if countryname =="Norway" & year ==2011
replace IFR =9781 if countryname =="Sweden" & year ==2011
replace IFR =290 if countryname =="Greece" & year ==2011
replace IFR =13 if countryname =="Iceland" & year ==2011
replace IFR =459 if countryname =="Ireland" & year ==2011

}
*2012
{
	
replace IFR =3597 if countryname =="Canada" & year ==2012
replace IFR =190321 if countryname =="United States" & year ==2012
replace IFR =7963 if countryname =="Australia" & year ==2012
replace IFR =620 if countryname =="New Zealand" & year ==2012
replace IFR =1474 if countryname =="Slovenia" & year ==2012
replace IFR =3301 if countryname =="Hungary" & year ==2012
replace IFR =2294 if countryname =="Slovakia" & year ==2012
replace IFR =51 if countryname =="Estonia" & year ==2012
replace IFR =6619 if countryname =="Austria" & year ==2012
replace IFR =6890 if countryname =="Belgium" & year ==2012
replace IFR =161988 if countryname =="Germany" & year ==2012
replace IFR =28911 if countryname =="Spain" & year ==2012
replace IFR =33624 if countryname =="France" & year ==2012
replace IFR =60750 if countryname =="Italy" & year ==2012
replace IFR =6718 if countryname =="Netherlands" & year ==2012
replace IFR =2524 if countryname =="Portugal" & year ==2012
replace IFR =5010 if countryname =="Switzerland" & year ==2012
replace IFR =15046 if countryname =="United Kingdom" & year ==2012
replace IFR =4613 if countryname =="Denmark" & year ==2012
replace IFR =4311 if countryname =="Finland" & year ==2012
replace IFR =1019 if countryname =="Norway" & year ==2012
replace IFR =9824 if countryname =="Sweden" & year ==2012
replace IFR =311 if countryname =="Greece" & year ==2012
replace IFR =13 if countryname =="Iceland" & year ==2012
replace IFR =522 if countryname =="Ireland" & year ==2012

}
*2013
{

replace IFR =5847 if countryname =="Canada" & year ==2013
replace IFR =203187 if countryname =="United States" & year ==2013
replace IFR =8016 if countryname =="Australia" & year ==2013
replace IFR =770 if countryname =="New Zealand" & year ==2013
replace IFR =1606 if countryname =="Slovenia" & year ==2013
replace IFR =3829 if countryname =="Hungary" & year ==2013
replace IFR =3572 if countryname =="Slovakia" & year ==2013
replace IFR =66 if countryname =="Estonia" & year ==2013
replace IFR =7009 if countryname =="Austria" & year ==2013
replace IFR =7998 if countryname =="Belgium" & year ==2013
replace IFR =167579 if countryname =="Germany" & year ==2013
replace IFR =28091 if countryname =="Spain" & year ==2013
replace IFR =32301 if countryname =="France" & year ==2013
replace IFR =59078 if countryname =="Italy" & year ==2013
replace IFR =7403 if countryname =="Netherlands" & year ==2013
replace IFR =2666 if countryname =="Portugal" & year ==2013
replace IFR =5270 if countryname =="Switzerland" & year ==2013
replace IFR =15591 if countryname =="United Kingdom" & year ==2013
replace IFR =4760 if countryname =="Denmark" & year ==2013
replace IFR =4268 if countryname =="Finland" & year ==2013
replace IFR =1005 if countryname =="Norway" & year ==2013
replace IFR =10164 if countryname =="Sweden" & year ==2013
replace IFR =344 if countryname =="Greece" & year ==2013
replace IFR =20 if countryname =="Iceland" & year ==2013
replace IFR =605 if countryname =="Ireland" & year ==2013
	
}
* 2014
{
	
replace IFR =8180 if countryname =="Canada" & year ==2014
replace IFR =219434 if countryname =="United States" & year ==2014
replace IFR =7927 if countryname =="Australia" & year ==2014
replace IFR =864 if countryname =="New Zealand" & year ==2014
replace IFR =1819 if countryname =="Slovenia" & year ==2014
replace IFR =4302 if countryname =="Hungary" & year ==2014
replace IFR =3891 if countryname =="Slovakia" & year ==2014
replace IFR =83 if countryname =="Estonia" & year ==2014
replace IFR =7237 if countryname =="Austria" & year ==2014
replace IFR =7995 if countryname =="Belgium" & year ==2014
replace IFR =175768 if countryname =="Germany" & year ==2014
replace IFR =27983 if countryname =="Spain" & year ==2014
replace IFR =32233 if countryname =="France" & year ==2014
replace IFR =59823 if countryname =="Italy" & year ==2014
replace IFR =8470 if countryname =="Netherlands" & year ==2014
replace IFR =2870 if countryname =="Portugal" & year ==2014
replace IFR =5764 if countryname =="Switzerland" & year ==2014
replace IFR =16935 if countryname =="United Kingdom" & year ==2014
replace IFR =5119 if countryname =="Denmark" & year ==2014
replace IFR =4178 if countryname =="Finland" & year ==2014
replace IFR =1008 if countryname =="Norway" & year ==2014
replace IFR =10742 if countryname =="Sweden" & year ==2014
replace IFR =392 if countryname =="Greece" & year ==2014
replace IFR =22 if countryname =="Iceland" & year ==2014
replace IFR =667 if countryname =="Ireland" & year ==2014

}
* 2015
{
	
replace IFR =11654 if countryname =="Canada" & year ==2015
replace IFR =234245 if countryname =="United States" & year ==2015
replace IFR =7742 if countryname =="Australia" & year ==2015
replace IFR =990 if countryname =="New Zealand" & year ==2015
replace IFR =2080 if countryname =="Slovenia" & year ==2015
replace IFR =4784 if countryname =="Hungary" & year ==2015
replace IFR =4378 if countryname =="Slovakia" & year ==2015
replace IFR =97 if countryname =="Estonia" & year ==2015
replace IFR =7859 if countryname =="Austria" & year ==2015
replace IFR =7989 if countryname =="Belgium" & year ==2015
replace IFR =182632 if countryname =="Germany" & year ==2015
replace IFR =29718 if countryname =="Spain" & year ==2015
replace IFR =32161 if countryname =="France" & year ==2015
replace IFR =61282 if countryname =="Italy" & year ==2015
replace IFR =9739 if countryname =="Netherlands" & year ==2015
replace IFR =3160 if countryname =="Portugal" & year ==2015
replace IFR =6258 if countryname =="Switzerland" & year ==2015
replace IFR =17469 if countryname =="United Kingdom" & year ==2015
replace IFR =5459 if countryname =="Denmark" & year ==2015
replace IFR =4124 if countryname =="Finland" & year ==2015
replace IFR =1068 if countryname =="Norway" & year ==2015
replace IFR =11857 if countryname =="Sweden" & year ==2015
replace IFR =446 if countryname =="Greece" & year ==2015
replace IFR =23 if countryname =="Iceland" & year ==2015
replace IFR =763 if countryname =="Ireland" & year ==2015

}
* 2016
{
	
replace IFR =13988 if countryname =="Canada" & year ==2016
replace IFR =250479 if countryname =="United States" & year ==2016
replace IFR =7536 if countryname =="Australia" & year ==2016
replace IFR =1105 if countryname =="New Zealand" & year ==2016
replace IFR =2452 if countryname =="Slovenia" & year ==2016
replace IFR =5424 if countryname =="Hungary" & year ==2016
replace IFR =6071 if countryname =="Slovakia" & year ==2016
replace IFR =123 if countryname =="Estonia" & year ==2016
replace IFR =9000 if countryname =="Austria" & year ==2016
replace IFR =8521 if countryname =="Belgium" & year ==2016
replace IFR =189305 if countryname =="Germany" & year ==2016
replace IFR =30811 if countryname =="Spain" & year ==2016
replace IFR =33384 if countryname =="France" & year ==2016
replace IFR =62068 if countryname =="Italy" & year ==2016
replace IFR =11320 if countryname =="Netherlands" & year ==2016
replace IFR =3942 if countryname =="Portugal" & year ==2016
replace IFR =6753 if countryname =="Switzerland" & year ==2016
replace IFR =18471 if countryname =="United Kingdom" & year ==2016
replace IFR =5915 if countryname =="Denmark" & year ==2016
replace IFR =4422 if countryname =="Finland" & year ==2016
replace IFR =1173 if countryname =="Norway" & year ==2016
replace IFR =12671 if countryname =="Sweden" & year ==2016
replace IFR =491 if countryname =="Greece" & year ==2016
replace IFR =31 if countryname =="Iceland" & year ==2016
replace IFR =880 if countryname =="Ireland" & year ==2016

}

*2017
{

replace IFR =18045 if countryname =="Canada" & year ==2017
replace IFR =262058 if countryname =="United States" & year ==2017
replace IFR =7126 if countryname =="Australia" & year ==2017
replace IFR =1172 if countryname =="New Zealand" & year ==2017
replace IFR =2805 if countryname =="Slovenia" & year ==2017
replace IFR =7711 if countryname =="Hungary" & year ==2017
replace IFR =7093 if countryname =="Slovakia" & year ==2017
replace IFR =170 if countryname =="Estonia" & year ==2017
replace IFR =10156 if countryname =="Austria" & year ==2017
replace IFR =9207 if countryname =="Belgium" & year ==2017
replace IFR =200497 if countryname =="Germany" & year ==2017
replace IFR =32352 if countryname =="Spain" & year ==2017
replace IFR =35321 if countryname =="France" & year ==2017
replace IFR =64403 if countryname =="Italy" & year ==2017
replace IFR =12505 if countryname =="Netherlands" & year ==2017
replace IFR =4622 if countryname =="Portugal" & year ==2017
replace IFR =7476 if countryname =="Switzerland" & year ==2017
replace IFR =19488 if countryname =="United Kingdom" & year ==2017
replace IFR =6361 if countryname =="Denmark" & year ==2017
replace IFR =4342 if countryname =="Finland" & year ==2017
replace IFR =1250 if countryname =="Norway" & year ==2017
replace IFR =13249 if countryname =="Sweden" & year ==2017
replace IFR =568 if countryname =="Greece" & year ==2017
replace IFR =42 if countryname =="Iceland" & year ==2017
replace IFR =945 if countryname =="Ireland" & year ==2017
	
}
*2018
{

replace IFR =21627 if countryname =="Canada" & year ==2018
replace IFR =285014 if countryname =="United States" & year ==2018
replace IFR =6927 if countryname =="Australia" & year ==2018
replace IFR =1220 if countryname =="New Zealand" & year ==2018
replace IFR =3414 if countryname =="Slovenia" & year ==2018
replace IFR =8481 if countryname =="Hungary" & year ==2018
replace IFR =7796 if countryname =="Slovakia" & year ==2018
replace IFR =220 if countryname =="Estonia" & year ==2018
replace IFR =11162 if countryname =="Austria" & year ==2018
replace IFR =9561 if countryname =="Belgium" & year ==2018
replace IFR =215795 if countryname =="Germany" & year ==2018
replace IFR =35209 if countryname =="Spain" & year ==2018
replace IFR =38079 if countryname =="France" & year ==2018
replace IFR =69142 if countryname =="Italy" & year ==2018
replace IFR =13385 if countryname =="Netherlands" & year ==2018
replace IFR =5050 if countryname =="Portugal" & year ==2018
replace IFR =8492 if countryname =="Switzerland" & year ==2018
replace IFR =20683 if countryname =="United Kingdom" & year ==2018
replace IFR =6617 if countryname =="Denmark" & year ==2018
replace IFR =4553 if countryname =="Finland" & year ==2018
replace IFR =1219 if countryname =="Norway" & year ==2018
replace IFR =13647 if countryname =="Sweden" & year ==2018
replace IFR =640 if countryname =="Greece" & year ==2018
replace IFR =37 if countryname =="Iceland" & year ==2018
replace IFR =1026 if countryname =="Ireland" & year ==2018

	
}
* 2019
{
replace IFR =25230 if countryname =="Canada" & year ==2019
replace IFR =299674 if countryname =="United States" & year ==2019
replace IFR =6649 if countryname =="Australia" & year ==2019
replace IFR =1278 if countryname =="New Zealand" & year ==2019
replace IFR =3941 if countryname =="Slovenia" & year ==2019
replace IFR =9212 if countryname =="Hungary" & year ==2019
replace IFR =8326 if countryname =="Slovakia" & year ==2019
replace IFR =283 if countryname =="Estonia" & year ==2019
replace IFR =12016 if countryname =="Austria" & year ==2019
replace IFR =10109 if countryname =="Belgium" & year ==2019
replace IFR =223387 if countryname =="Germany" & year ==2019
replace IFR =36916 if countryname =="Spain" & year ==2019
replace IFR =42054 if countryname =="France" & year ==2019
replace IFR =74420 if countryname =="Italy" & year ==2019
replace IFR =14370 if countryname =="Netherlands" & year ==2019
replace IFR =5620 if countryname =="Portugal" & year ==2019
replace IFR =9506 if countryname =="Switzerland" & year ==2019
replace IFR =21678 if countryname =="United Kingdom" & year ==2019
replace IFR =6824 if countryname =="Denmark" & year ==2019
replace IFR =4728 if countryname =="Finland" & year ==2019
replace IFR =1271 if countryname =="Norway" & year ==2019
replace IFR =14224 if countryname =="Sweden" & year ==2019
replace IFR =665 if countryname =="Greece" & year ==2019
replace IFR =37 if countryname =="Iceland" & year ==2019
replace IFR =1130 if countryname =="Ireland" & year ==2019
	
}

// Now the log of the number of industrial robots
gen IFR2 = ln(IFR)
}
}

*############################################
* Saving the data
*############################################
{
lab var IFR2 "Robots Stock"
lab var PRITM "PR with Trichotomous Multipartism"
lab var totseats "Total Number of Seats"
lab var number2 "Total Number of Parties"
lab var oecdmember "OECD member"
lab var distance_redist "Distance Redistribution (DR) - Net Welfare"
lab var distance_fixed "Distance Fixed-Value Positions (DFVP) - Net Anti-Global"
lab var IFR2 "Robots Stock"

lab var distance_fixed_eu "DFVP - Net Anti-EU"
lab var distance_nat "DFVP - Net Anti-Global Narrow (Internationalism)"
lab var distance_fixed_all "DFVP - Anti-Global and Cultural"

lab var shock "High LMP period"

keep  PRITM year  countryname  oecdmember totseats shock distance_redist distance_fixed  distance_fixed_eu  distance_nat  distance_fixed_all distance_fixed_nolog IFR IFR2 number2 election_order country_number

keep if year>1969
keep if PRITM==1

save "Data\CMP_main.dta", replace
}
}

```



```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Alternatively load prepared data
*##########################################

use "Data\CMP_main.dta", clear	
	
```


#### Table 3: PRITM: Partisan Polarization over Redistribution and Fixed Attributes


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
// table 3: PRITM: Partisan Polarization over Redistribution and Fixed Attributes
{
preserve

eststo clear
eststo: qui reg distance_redist L.distance_redist shock totseats   oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_redist L.distance_redist IFR2 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed IFR2 totseats  oecdmember   i.year, cluster(countryname)



esttab , replace label se title(Polarization over Redistribution and Fixed Attributes  \label {TableCMPAll}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock IFR2) scalars(N r2 aic) indicate("LDV = L.*" "FE Year = *year")


restore

}
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\TabWithin.tex", replace label se title(Polarization over Redistribution and Fixed Attributes  \label {TableCMPAll}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock IFR2) scalars(N r2 aic) indicate("LDV = L.*" "FE Year = *year")

```

```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{3}

% Include the table with custom numbering
\input{Table/TabWithin.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```








#### Table A19: Partisan Polarization over Redistribution and Fixed Attributes Different Cut-Off


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
// table A19: Partisan Polarization over Redistribution and Fixed Attributes Different Cut-Of
{
gen shock92=.
replace shock92 =0 if year > 1969 & year < 1993
replace shock92 =1 if  year > 1992

gen shock93=.
replace shock93 =0 if year > 1969 & year < 1994
replace shock93 =1 if  year > 1993

gen shock96=.
replace shock96 =0 if year > 1969 & year < 1996
replace shock96 =1 if  year > 1997

gen shock97=.
replace shock97 =0 if year > 1969 & year < 1997
replace shock97 =1 if  year > 1998

gen shock98=.
replace shock98 =0 if year > 1969 & year < 1998
replace shock98 =1 if  year > 1999


lab var shock98 "Post-LMP"
lab var shock92 "Post-LMP"
lab var shock93 "Post-LMP"
lab var shock96 "Post-LMP"
lab var shock97 "Post-LMP"


preserve
  
eststo clear
eststo: qui reg distance_redist L.distance_redist shock92 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock92 totseats  oecdmember   i.year, cluster(countryname)

eststo: qui reg distance_redist L.distance_redist shock93 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock93 totseats  oecdmember   i.year, cluster(countryname)


eststo: qui reg distance_redist L.distance_redist shock96 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock96 totseats  oecdmember   i.year, cluster(countryname)


eststo: qui reg distance_redist L.distance_redist shock97 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock97 totseats  oecdmember   i.year, cluster(countryname)


eststo: qui reg distance_redist L.distance_redist shock98 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock98 totseats  oecdmember   i.year, cluster(countryname)

esttab , replace label se title(Partisan Polarization over Redistribution and Fixed Attributes Different Cut-Off \label {Tablewithincut}) mti("Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock*     ) scalars( "N Observations" "r2 R$^2$" "aic AIC" )  indicate("LDV = L.*" "FE Year = *year")


restore	
}
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\TabWithin_cutoff.tex", replace label se title(Partisan Polarization over Redistribution and Fixed Attributes Different Cut-Off \label {Tablewithincut}) mti("Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock*     ) scalars( "N Observations" "r2 R$^2$" "aic AIC" )  indicate("LDV = L.*" "FE Year = *year")

```

```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A19}

% Include the table with custom numbering
\input{Table/TabWithin_cutoff.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```









#### Table A20: Alternative measures of Partisan Polarization over Fixed Attributes between Mainstream Left and Right-Populist


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}

lab var distance_fixed_eu "Anti-EU"
lab var distance_nat "Internationalism"
lab var distance_fixed_all "Anti-Global and Cultural"
lab var distance_fixed_nolog "Anti-Global and Cultural (no log)"


// table A20: Alternative measures of Partisan Polarization over Fixed Attributes between Mainstream Left and Right-Populist
{
preserve

eststo clear

foreach x of varlist distance_fixed_eu  distance_nat  distance_fixed_all distance_fixed_nolog {
eststo: qui reg `x' L.`x' shock totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg `x' L.`x' IFR2 totseats  oecdmember   i.year, cluster(countryname)

}
esttab, replace label se title(Alternative measures of Partisan Polarization over Fixed Attributes between Mainstream Left and Right-Populist \label {FVPritm})  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock IFR2 ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tot*" "LDV = L.*" "FE Year = *year") 


restore

}
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\TabWithin_FValternative.tex", replace label se title(Alternative measures of Partisan Polarization over Fixed Attributes between Mainstream Left and Right-Populist \label {FVPritm})  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock IFR2 ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tot*" "LDV = L.*" "FE Year = *year") 

```

```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A20}

% Include the table with custom numbering
\input{Table/TabWithin_FValternative.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```







#### Table A18: Descriptive statistic: PRITM 1970-2019


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Descriptive 
*##########################################

lab var distance_fixed_eu "DFVP - Net Anti-EU"
lab var distance_nat "DFVP - Net Anti-Global Narrow (Internationalism)"
lab var distance_fixed_all "DFVP - Anti-Global and Cultural"
lab var distance_fixed_nolog "DFVP - Anti-Global and Cultural (no log)"


// table A18: Descriptive statistic: PRITM 1970-2019
{
lab var IFR "\# Robot Stock (IFR)"
lab var IFR2 "Ln \# Robot Stock (IFR)"
lab var distance_fixed_nolog "DFVP - Anti-Global and Cultural (no log)"
preserve

	eststo clear

qui estpost sum  totseats number2 oecdmember distance_redist  distance_fixed  distance_fixed_eu   distance_fixed_all distance_nat  distance_fixed_all distance_fixed_nolog IFR IFR2, d 



	
	restore
	}

```
```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, eval=TRUE}

esttab ,cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs  title(Descriptive statistic: PRITM 1970-2019 \label {SummarystatPRITM}) 

esttab using "Table\desc_PRITM.tex",cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs  title(Descriptive statistic: PRITM 1970-2019 \label {SummarystatPRITM}) 
```



```{=latex}
\renewcommand{\thetable}{A18}

\input{Table/desc_PRITM.tex}


\renewcommand{\thetable}{\arabic{table}}

```











### CMP Appendix - average distance [3_3_CMP_PRITM_Appendix_Average.do] 

This do-file:

	A. Call the Data
	B. Define variables
	C. Export Tables 

Input: **Manifesto Project database**

	- `Data\CMP\MPDataset_MPDS2020a_stata14.dta` // Data download from https://manifesto-project.wzb.eu/datasets 2021


Final output:

*	Cleaned data: 
		* `Data\CMP_average.dta` this data contains the relevant variables for the analysis with the DV as the polarization over redistribution, and fixed-value positions, estimated as the average distance of the party system.
*	Tables:
		* table A21: Partisan Polarization over Redistribution and Fixed Attributes
		
		
```{stata, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE}
* Processing of the data (alternatively skip and go to line 158)
{
*##########################################
* A. Calling the data
*##########################################
{

use "Data\CMP\MPDataset_MPDS2020a_stata14.dta", replace

}
*############################################
* B. Creating Variables
*############################################
{
* YEAR
gen year = year(edate) 

* Keep countries of interest
keep if countryname=="Australia" |  countryname=="Canada" |  countryname=="Greece" |  countryname=="New Zealand" |  countryname=="Portugal" |  countryname=="Spain" |  countryname=="United Kingdom" |  countryname=="United States" |  countryname=="France" |  countryname=="Norway" | countryname=="Austria" | countryname=="Belgium" | countryname=="Denmark" | countryname=="Estonia" | countryname=="Finland" | countryname=="Hungary" | countryname=="Germany" | countryname=="Iceland"  | countryname=="Ireland"  | countryname=="Italy"  | countryname=="Netherlands"  | countryname=="Norway" | countryname=="Slovakia" | countryname=="Slovenia" | countryname=="Sweden" | countryname=="Switzerland"

* Defines a dummy to identify PRITM countries
gen PRITM=. 
replace PRITM=0 if countryname=="Australia"
replace PRITM=0 if countryname=="Canada"
replace PRITM=0 if countryname=="Greece"
replace PRITM=0 if countryname=="New Zealand"
replace PRITM=0 if countryname=="Portugal"
replace PRITM=0 if countryname=="Spain"
replace PRITM=0 if countryname=="United Kingdom"
replace PRITM=0 if countryname=="United States"
replace PRITM=0 if countryname=="France"

replace PRITM=1 if countryname=="Austria"
replace PRITM=1 if countryname=="Belgium"
replace PRITM=1 if countryname=="Denmark"
replace PRITM=1 if countryname=="Estonia"
replace PRITM=1 if countryname=="Finland"
replace PRITM=1 if countryname=="Germany"
replace PRITM=1 if countryname=="Hungary"
replace PRITM=1 if countryname=="Iceland"
replace PRITM=1 if countryname=="Ireland"
replace PRITM=1 if countryname=="Italy"
replace PRITM=1 if countryname=="Netherlands"
replace PRITM=1 if countryname=="Norway"
replace PRITM=1 if countryname=="Slovenia"
replace PRITM=1 if countryname=="Slovakia"
replace PRITM=1 if countryname=="Sweden"
replace PRITM=1 if countryname=="Switzerland"


* CONTROL VARIABLES - number of parties

gen number = 1
egen number2= sum(number), by(edate)
lab var number2 "Number of parties"

*############################################################
* DEPENDENT VARIABLE Redistribution and Fixed Attributes
*############################################################
{
* Obtaining relevant policy variables

gen welfare_policy =  ln(per504+0.5) - ln(per505+0.5)

gen fixed = .
replace fixed = (ln(per107+per108+per407+per602+0.5+per602_2)-ln(per109+per110+per406+per601+0.5+per601_2)) if !missing(per602_2) & !missing(per601_2)
replace fixed = (ln(per107+per108+per407+per602+0.5)-ln(per109+per110+per406+per601+0.5)) if missing(per602_2) | missing(per601_2)

}

** Different code compared to 3_2 do file: 
***** Now average distance instead of distance between certain party families
{
egen av_welfare_policy= mean(welfare_policy), by(edate countryname)
gen dist_av_welfare_policy = abs(welfare_policy-av_welfare_policy)

egen av_fixed= mean(fixed), by(edate countryname)
gen dist_av_fixed = abs(fixed-av_fixed)

}
* Collapsing the data 
{
collapse (sum) av_welfare_policy dist_av_welfare_policy   av_fixed dist_av_fixed  (first) year  PRITM  totseats number2 oecdmember date  , by(edate countryname)
}
// Final Prep of the data
{
egen country_number = group(countryname)
sort country_number edate
bysort country_number: gen election_order=_n
xtset country_number election_order
sort countryname  election_order
sort countryname  year

gen shock=.
replace shock =0 if year > 1969 & year < 1995
replace shock =1 if  year > 1994
xtset country_number election_order
}
}
*############################################
* Saving the data
*############################################
{

lab var PRITM "PR with Trichotomous Multipartism"
lab var totseats "Total Number of Seats"
lab var number2 "Total Number of Parties"
lab var oecdmember "OECD member"
lab var PRITM "PRITM"

lab var dist_av_welfare_policy "Distance Redistribution (DR) - Net Welfare"
lab var dist_av_fixed "Distance Fixed-Value Positions (DFVP) - Net Anti-Global"

lab var shock "High LMP period" // post 1994

keep  PRITM year  countryname  oecdmember totseats shock dist_av_welfare_policy dist_av_fixed   number2 election_order country_number

keep if year>1969
keep if PRITM==1

save "Data\CMP_average.dta", replace

}
}


```




```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Alternatively load prepared data
*##########################################

use "Data\CMP_average.dta", clear	
	
```


#### Table A21: Partisan Polarization over Redistribution and Fixed Attributes


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Analysis
*##########################################
// table A21: Partisan Polarization over Redistribution and Fixed Attributes
{

preserve
 
eststo clear
eststo: qui reg dist_av_welfare_policy L.dist_av_welfare_policy shock totseats  oecdmember  number2 i.year, cluster(countryname)
eststo: qui reg dist_av_fixed L.dist_av_fixed shock totseats  oecdmember  number2 i.year, cluster(countryname)

esttab , replace label se title(Partisan Polarization over Redistribution and Fixed Attributes  \label {TabWithinpoldistav}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock  ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tots*" "LDV = L.*" "FE Year = *year") 




restore

}

```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\Tab_Within_d_av.tex", replace label se title(Partisan Polarization over Redistribution and Fixed Attributes  \label {TabWithinpoldistav}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock  ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tots*" "LDV = L.*" "FE Year = *year") 

```

```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A21}

% Include the table with custom numbering
\input{Table/Tab_Within_d_av.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```










### CMP Appendix - Dalton index [3_4_CMP_PRITM_Appendix_Dalton.do] 

This do-file:

	A. Call the Data
	B. Define variables
	C. Export Tables 

Input: ** Manifesto Project database**

	- `Data\CMP\MPDataset_MPDS2020a_stata14.dta` // Data download from https://manifesto-project.wzb.eu/datasets 2021


Final output:

*	Cleaned data: 
		* `Data\CMP_Dalton.dta` this data contains the relevant variables for the analysis with the DV as the polarization over redistribution, and fixed-value positions, estimated as Dalton index
*	Tables:
		* table A22: Partisan Polarization over Redistribution and Fixed Attributes, Dalton Index



```{stata, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE}
* Processing of the data (alternatively skip and go to line 175)
{
*##########################################
* A. Calling the data
*##########################################
{

use "Data\CMP\MPDataset_MPDS2020a_stata14.dta", replace

}

*############################################
* B. Creating Variables
*############################################
{
* YEAR
gen year = year(edate) 

* Keep countries of interest
keep if countryname=="Australia" |  countryname=="Canada" |  countryname=="Greece" |  countryname=="New Zealand" |  countryname=="Portugal" |  countryname=="Spain" |  countryname=="United Kingdom" |  countryname=="United States" |  countryname=="France" |  countryname=="Norway" | countryname=="Austria" | countryname=="Belgium" | countryname=="Denmark" | countryname=="Estonia" | countryname=="Finland" | countryname=="Hungary" | countryname=="Germany" | countryname=="Iceland"  | countryname=="Ireland"  | countryname=="Italy"  | countryname=="Netherlands"  | countryname=="Norway" | countryname=="Slovakia" | countryname=="Slovenia" | countryname=="Sweden" | countryname=="Switzerland"

* Defines a dummy to identify PRITM countries
gen PRITM=. 
replace PRITM=0 if countryname=="Australia"
replace PRITM=0 if countryname=="Canada"
replace PRITM=0 if countryname=="Greece"
replace PRITM=0 if countryname=="New Zealand"
replace PRITM=0 if countryname=="Portugal"
replace PRITM=0 if countryname=="Spain"
replace PRITM=0 if countryname=="United Kingdom"
replace PRITM=0 if countryname=="United States"
replace PRITM=0 if countryname=="France"

replace PRITM=1 if countryname=="Austria"
replace PRITM=1 if countryname=="Belgium"
replace PRITM=1 if countryname=="Denmark"
replace PRITM=1 if countryname=="Estonia"
replace PRITM=1 if countryname=="Finland"
replace PRITM=1 if countryname=="Germany"
replace PRITM=1 if countryname=="Hungary"
replace PRITM=1 if countryname=="Iceland"
replace PRITM=1 if countryname=="Ireland"
replace PRITM=1 if countryname=="Italy"
replace PRITM=1 if countryname=="Netherlands"
replace PRITM=1 if countryname=="Norway"
replace PRITM=1 if countryname=="Slovenia"
replace PRITM=1 if countryname=="Slovakia"
replace PRITM=1 if countryname=="Sweden"
replace PRITM=1 if countryname=="Switzerland"


* CONTROL VARIABLES - number of parties

gen number = 1
egen number2= sum(number), by(edate)
lab var number2 "Number of parties"

*############################################################
* DEPENDENT VARIABLE Redistribution and Fixed Attributes
*############################################################
{
* Obtaining relevant policy variables

gen welfare_policy =  ln(per504+0.5) - ln(per505+0.5)

gen fixed = .
replace fixed = (ln(per107+per108+per407+per602+0.5+per602_2)-ln(per109+per110+per406+per601+0.5+per601_2)) if !missing(per602_2) & !missing(per601_2)
replace fixed = (ln(per107+per108+per407+per602+0.5)-ln(per109+per110+per406+per601+0.5)) if missing(per602_2) | missing(per601_2)

}

** Different code compared to 3_2 do file: 
***** Now Dalton Index
{
egen pervote_e= sum(pervote), by(edate partyname)

foreach x of varlist welfare_policy fixed  {

egen `x'_av= mean(`x'), by(edate countryname)
}	

foreach x of varlist welfare_policy fixed  {

gen `x'_d= pervote_e*(`x'-`x'_av)^2
}

foreach x of varlist welfare_policy fixed  {

egen `x'_dt= sum(`x'_d), by(edate countryname)
gen `x'_dt2= (`x'_dt)^0.5
}	
}

* Collapsing the data 
{
collapse (first) year  PRITM  totseats number2 oecdmember date *_dt2 *_dt , by(edate countryname)
}
// Final Prep of the data
{
egen country_number = group(countryname)

sort country_number edate
bysort country_number: gen election_order=_n
xtset country_number election_order


sort countryname  election_order




sort countryname  year

gen shock=.
replace shock =0 if year > 1969 & year < 1995
replace shock =1 if  year > 1994
xtset country_number election_order
}
}
*############################################
* Saving the data
*############################################
{
lab var PRITM "PR with Trichotomous Multipartism"
lab var totseats "Total Number of Seats"
lab var number2 "Total Number of Parties"
lab var oecdmember "OECD member"
lab var welfare_policy_dt2 "Distance Redistribution (DR) - Net Welfare"
lab var fixed_dt2 "Distance Fixed-Value Positions (DFVP) - Net Anti-Global"
lab var PRITM "PRITM"
lab var shock "High LMP period" // post 1994

keep  PRITM year  countryname  oecdmember totseats shock welfare_policy_dt2 fixed_dt2   number2 election_order country_number

keep if year>1969
keep if PRITM==1

save "Data\CMP_Dalton.dta", replace

}
}


```




```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Alternatively load prepared data
*##########################################

use "Data\CMP_Dalton.dta", clear	
	
```


#### Table A22: Partisan Polarization over Redistribution and Fixed Attributes, Dalton Index


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Analysis
*##########################################
{
// table A22: Partisan Polarization over Redistribution and Fixed Attributes, Dalton Index
{
preserve
keep if PRITM==1
   keep if year>1969 

eststo clear
// Redistribution
eststo: qui reg welfare_policy_dt2 L.welfare_policy_dt2 shock totseats  oecdmember  number2 i.year, cluster(countryname)
eststo: qui reg fixed_dt2 L.fixed_dt2 shock totseats  oecdmember  number2 i.year, cluster(countryname)



esttab , replace label se title(Polarization over Redistribution and Fixed Attributes, Dalton Index \label {TabWithindalton}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tots*" "LDV = L.*" "FE Year = *year") 




restore
}
}
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\TabWithindalton.tex", replace label se title(Polarization over Redistribution and Fixed Attributes, Dalton Index \label {TabWithindalton}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tots*" "LDV = L.*" "FE Year = *year") 

```

```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A22}

% Include the table with custom numbering
\input{Table/TabWithindalton.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```







### CMP Appendix - ER [3_5_CMP_PRITM_Appendix_ER.do] 

This do-file:

	A. Call the Data
	B. Define variables
	C. Export Tables 

Input: ** Manifesto Project database**

	- `Data\CMP\MPDataset_MPDS2020a_stata14.dta` // Data download from https://manifesto-project.wzb.eu/datasets 2021


Final output:

*	Cleaned data:

		* `Data\CMP_ER.dta` this data contains the relevant variables for the analysis with the DV as the polarization over redistribution, and fixed-value positions, estimated following Esteban and Ray 1994.
		
*	Tables:

		* table A23: Partisan Polarization over Redistribution and Fixed Attributes


```{stata, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE}
* Processing of the data (alternatively skip and go to line 156)
{
*##########################################
* A. Calling the data
*##########################################
{

use "Data\CMP\MPDataset_MPDS2020a_stata14.dta", replace

}
*############################################
* B. Creating Variables
*############################################
{
* YEAR
gen year = year(edate) 

* Keep countries of interest
keep if countryname=="Australia" |  countryname=="Canada" |  countryname=="Greece" |  countryname=="New Zealand" |  countryname=="Portugal" |  countryname=="Spain" |  countryname=="United Kingdom" |  countryname=="United States" |  countryname=="France" |  countryname=="Norway" | countryname=="Austria" | countryname=="Belgium" | countryname=="Denmark" | countryname=="Estonia" | countryname=="Finland" | countryname=="Hungary" | countryname=="Germany" | countryname=="Iceland"  | countryname=="Ireland"  | countryname=="Italy"  | countryname=="Netherlands"  | countryname=="Norway" | countryname=="Slovakia" | countryname=="Slovenia" | countryname=="Sweden" | countryname=="Switzerland"

* Defines a dummy to identify PRITM countries
gen PRITM=. 
replace PRITM=0 if countryname=="Australia"
replace PRITM=0 if countryname=="Canada"
replace PRITM=0 if countryname=="Greece"
replace PRITM=0 if countryname=="New Zealand"
replace PRITM=0 if countryname=="Portugal"
replace PRITM=0 if countryname=="Spain"
replace PRITM=0 if countryname=="United Kingdom"
replace PRITM=0 if countryname=="United States"
replace PRITM=0 if countryname=="France"

replace PRITM=1 if countryname=="Austria"
replace PRITM=1 if countryname=="Belgium"
replace PRITM=1 if countryname=="Denmark"
replace PRITM=1 if countryname=="Estonia"
replace PRITM=1 if countryname=="Finland"
replace PRITM=1 if countryname=="Germany"
replace PRITM=1 if countryname=="Hungary"
replace PRITM=1 if countryname=="Iceland"
replace PRITM=1 if countryname=="Ireland"
replace PRITM=1 if countryname=="Italy"
replace PRITM=1 if countryname=="Netherlands"
replace PRITM=1 if countryname=="Norway"
replace PRITM=1 if countryname=="Slovenia"
replace PRITM=1 if countryname=="Slovakia"
replace PRITM=1 if countryname=="Sweden"
replace PRITM=1 if countryname=="Switzerland"


* CONTROL VARIABLES - number of parties

gen number = 1
egen number2= sum(number), by(edate)
lab var number2 "Number of parties"

*############################################################
* DEPENDENT VARIABLE Redistribution and Fixed Attributes
*############################################################
* Obtaining relevant policy variables
{
gen welfare_policy =  ln(per504+0.5) - ln(per505+0.5)

gen fixed = .
replace fixed = (ln(per107+per108+per407+per602+0.5+per602_2)-ln(per109+per110+per406+per601+0.5+per601_2)) if !missing(per602_2) & !missing(per601_2)
replace fixed = (ln(per107+per108+per407+per602+0.5)-ln(per109+per110+per406+per601+0.5)) if missing(per602_2) | missing(per601_2)

}
** Different code compared to 3_2 do file: 
{
**  Mainstream left: - soc social democratic and socialist or other left
gen m_left = . 
replace m_left = 1 if parfam== 30 | parfam== 20
replace m_left = 0 if parfam ~= 30 & parfam ~=20 & parfam ~=. 

egen m_left_participation= max(m_left), by(edate)


**  Mainstream left: only soc social democratic
gen m_left_restrict = . 
replace m_left_restrict = 1 if parfam== 30
replace m_left_restrict = 0 if parfam ~= 30 & parfam ~=. 

** Other left: Ecologista and socialist
gen o_left = . 
replace o_left = 1 if  parfam== 10  // here I am also including socialist or other left
replace o_left = 0 if parfam ~= 10  & parfam ~=. 

egen o_left_participation= max(o_left), by(edate)

** Mainstream right: lib liberal, Christian Democrat, Conservatives
gen m_right = . 
replace m_right = 1 if parfam == 40 | parfam == 50 | parfam == 60
replace m_right = 0 if parfam ~= 40 & parfam ~= 50 & parfam ~= 60 & parfam ~=. 

egen m_right_participation= max(m_right), by(edate)

** Other right: agriculture
gen o_right = . 
replace o_right = 1 if parfam == 80
replace o_right = 0 if parfam ~= 80 & parfam ~=. 

egen o_right_participation= max(o_right), by(edate)

** Radical right:  nat nationalist 
gen rad_right = . 
replace rad_right = 1 if parfam == 70 
replace rad_right = 0 if parfam ~= 70 & parfam ~=. 

egen rad_right_participation= max(rad_right), by(edate)


** Other parties: special issues and Ethnic and regional parties
gen o_parties = . 
replace o_parties = 1 if parfam == 90 | parfam == 95 
replace o_parties = 0 if parfam ~= 90 & parfam ~= 95  & parfam ~=. 

egen o_parties_participation= max(o_parties), by(edate)


** DISTANCE
{
foreach x of varlist welfare_policy  fixed  {

gen `x'_m_left = m_left*`x' if m_left==1
gen `x'_rad_right = rad_right*`x' if rad_right==1
gen `x'_m_right = m_right*`x' if m_right==1

gen `x'_o_left = o_left*`x' if o_left==1
gen `x'_o_right = o_right*`x' if o_right==1
gen `x'_o_parties = o_parties*`x' if o_parties==1

replace `x'_m_left = 0 if `x'_m_left ==.
replace `x'_rad_right = 0 if `x'_rad_right ==.
replace `x'_m_right = 0 if `x'_m_right ==.
replace `x'_o_left = 0 if `x'_o_left ==.
replace `x'_o_right = 0 if `x'_o_right ==.
replace `x'_o_parties = 0 if `x'_o_parties ==.
	
}

foreach x of varlist m_left rad_right m_right o_left o_right o_parties {

egen vote_`x' = mean(pervote), by(countryname edate `x')
replace vote_`x'=. if `x'==0
}



sort countryname edate
}
* Collapsing the data 
{
collapse (sum) fixed* welf*  (first) *_participation year  PRITM  totseats number2 oecdmember date (mean) vote_* , by(edate countryname)
}
// Final Prep of the data
{
egen country_number = group(countryname)

sort country_number edate
bysort country_number: gen election_order=_n
xtset country_number election_order


gen alpha=1.6




foreach x of varlist  welfare_policy fixed {

* Distance
// Mainstream left - other left
gen `x'_ml_ol =  vote_m_left^(alpha)*vote_o_left^(alpha)*abs(`x'_m_left -`x'_o_left)
replace `x'_ml_ol =  0 if o_left_participation==0

// Mainstream left - mainstream right
gen `x'_ml_mr =  vote_m_left^(alpha)*vote_m_right^(alpha)*abs(`x'_m_left -`x'_m_right)
replace `x'_ml_mr =  0 if m_right_participation==0

// Mainstream left - radical right
gen `x'_ml_rr =  vote_m_left^(alpha)*vote_rad_right^(alpha)*abs(`x'_m_left -`x'_rad_right)
replace `x'_ml_rr =  0 if rad_right_participation==0

// Mainstream left - other right
gen `x'_ml_or =  vote_m_left^(alpha)*vote_o_right^(alpha)*abs(`x'_m_left -`x'_o_right)
replace `x'_ml_or =  0 if o_right_participation==0

// Mainstream left - other parties
gen `x'_ml_op =  vote_m_left^(alpha)*vote_o_parties^(alpha)*abs(`x'_m_left -`x'_o_parties)
replace `x'_ml_op =  0 if o_parties_participation==0

//////
// Mainstream right - other left
gen `x'_mr_ol =  vote_m_right^(alpha)*vote_o_left^(alpha)*abs(`x'_m_right -`x'_o_left)
replace `x'_mr_ol =  0 if o_left_participation==0

// Mainstream right - radical right
gen `x'_mr_rr =  vote_m_right^(alpha)*vote_rad_right^(alpha)*abs(`x'_m_right -`x'_rad_right)
replace `x'_mr_rr =  0 if rad_right_participation==0

// Mainstream right - other right
gen `x'_mr_or =  vote_m_right^(alpha)*vote_o_right^(alpha)*abs(`x'_m_right -`x'_o_right)
replace `x'_mr_or =  0 if o_right_participation==0

// Mainstream right - other party
gen `x'_mr_op =  vote_m_right^(alpha)*vote_o_parties^(alpha)*abs(`x'_m_right -`x'_o_parties)
replace `x'_mr_op =  0 if `x'_mr_op==.


///// Radical right
// Radical right - other left
gen `x'_rr_ol =  vote_rad_right^(alpha)*vote_o_left^(alpha)*abs(`x'_rad_right -`x'_o_left)
replace `x'_rr_ol =  0 if o_left_participation==0

// Radical right - other right
gen `x'_rr_or =  vote_rad_right^(alpha)*vote_o_right^(alpha)*abs(`x'_rad_right -`x'_o_right)
replace `x'_rr_or =  0 if o_right_participation==0

// Radical right - other party
gen `x'_rr_op =  vote_rad_right^(alpha)*vote_o_parties^(alpha)*abs(`x'_rad_right -`x'_o_parties)
replace `x'_rr_op =  0 if o_parties_participation==0

///// Other left
// Other left - other right
gen `x'_ol_or =  vote_o_left^(alpha)*vote_o_parties^(alpha)*abs(`x'_o_left -`x'_o_right)
replace `x'_ol_or =  0 if o_right_participation==0

// Other left - other party
gen `x'_ol_op =  vote_o_left^(alpha)*vote_o_parties^(alpha)*abs(`x'_o_left -`x'_o_parties)
replace `x'_ol_op =  0 if o_parties_participation==0

//// Other right
// Other right- other party
gen `x'_or_op =  vote_o_right^(alpha)*vote_o_parties^(alpha)*abs(`x'_o_right -`x'_o_parties)
replace `x'_or_op =  0 if o_parties_participation==0

gen distance_`x' = `x'_ml_ol+ `x'_ml_mr+ `x'_ml_rr+ `x'_ml_or+ `x'_ml_op+ `x'_mr_ol+ `x'_mr_rr+ `x'_mr_or +`x'_mr_op+ `x'_rr_ol+ `x'_rr_or+ `x'_rr_op+ `x'_ol_or+ `x'_ol_op+ `x'_or_op

gen distance_`x'2 = `x'_ml_mr+ `x'_ml_rr+ `x'_ml_or+ `x'_mr_rr+ `x'_mr_or  + `x'_rr_or
gen distance_`x'3 = `x'_ml_mr+ `x'_ml_rr+ `x'_mr_rr  





}

gen shock=.
replace shock =0 if year > 1969 & year < 1995
replace shock =1 if  year > 1994	
}
}
}
*############################################
* Saving the data
*############################################
{

lab var PRITM "PR with Trichotomous Multipartism"
lab var totseats "Total Number of Seats"
lab var number2 "Total Number of Parties"
lab var oecdmember "OECD member"
lab var PRITM "PRITM"

lab var distance_welfare_policy3 "Distance Redistribution (DR) - Net Welfare"
lab var distance_fixed3 "Distance Fixed-Value Positions (DFVP) - Net Anti-Global"

lab var shock "High LMP period" // post 1994

keep  PRITM year  countryname  oecdmember totseats shock distance_welfare_policy3 distance_fixed3   number2 election_order country_number

keep if year>1969
keep if PRITM==1

save "Data\CMP_ER.dta", replace

}
}

```




```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Alternatively load prepared data
*##########################################

use "Data\CMP_ER.dta", clear	
	
```


#### Table A23: Partisan Polarization over Redistribution and Fixed Attributes


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Analysis
*##########################################
{

// table A23: Partisan Polarization over Redistribution and Fixed Attributes
{
preserve

eststo clear
eststo: qui reg distance_welfare_policy3 L.distance_welfare_policy3 shock totseats  oecdmember  number2 , cluster(countryname)
eststo: qui reg distance_fixed3 L.distance_fixed3 shock totseats  oecdmember  number2 , cluster(countryname)



esttab , replace label se title(Partisan Polarization over Redistribution and Fixed Attributes  \label {TabWithinER}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock    ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Control variables = tot*" "LDV = L.*" )


restore
}
}
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\TabWithin_ER.tex", replace label se title(Partisan Polarization over Redistribution and Fixed Attributes  \label {TabWithinER}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock    ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Control variables = tot*" "LDV = L.*" )

```

```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{A23}

% Include the table with custom numbering
\input{Table/TabWithin_ER.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```














## PRITM Systems: Campaign Message and Targeting Strategies in Germany 

### Topic Analysis [2_3_Speech_US_Germany_Appendix_NMF.ipynb]  


This file:

- Creates inputs for Table A17: NMF Topic Modeling.
- First it does the NMF for the US, then for Germany.
- Once topic are created the proportion of each one of them is calculated. 


Input:

- `Data/cleaned_data.csv` # Data for the US
- `Data/cleaned_data_G.csv` # Data for Germany


Output:

- Inputs for Table A17: NMF Topic Modeling, 4 clusters, top-10 terms.


**NOTE: the code for this file can be accessed in the jupyter notebook in the folder `do`.**


###  Targeting Strategies: Electoral Performance Across Districts  [3_0_Regional_Germany_HateIncidents.rmd] & [3_1_Regional_Germany.do]

#### [3_0_Regional_Germany_HateIncidents.rmd]





This Rmarkdown:

- Creates a file with the data for hate incidents by region in Germany. 



Input:

- Download `arvig` data from the package
  

Output:

- `Data/final_aggregated_data.dta`   It is a dta file with the count of hate incidents per geographic area. 


```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("davben/arvig")
```


```{r usage, echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
library(arvig)
library(tidyverse)
library(sf)

data("arvig")
```



```{r, echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
## Preparing the data
#I will count the events, and create a single variable called anti, which refers to the count of incidents. 

data(arvig, package = "arvig")
arvig$count<-1
arvig <- arvig %>%
  mutate(attack = case_when(
    category_en %in% c("arson & miscellaneous attack", 
                       "demonstration & miscellaneous attack", 
                       "assault", 
                       "miscellaneous attack", 
                       "miscellaneous attack & assault", 
                       "arson", 
                       "other") ~ 1,
    TRUE ~ 0 # Assigns 0 if none of the conditions above are met
  ))

arvig <- arvig %>%
  mutate(demonstration = case_when(
    category_en %in% c("demonstration", 
                       "demonstration & miscellaneous attack", 
                       "suspicion") ~ 1,
    TRUE ~ 0 # Assigns 0 if none of the conditions above are met
  ))



```

Common format to merge

To merge the data with the other sources I need to modify community_id and call it kreis. 

```{r, echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
arvig$kreis <- ifelse(substr(arvig$community_id, 1, 1) == "0", 
                           substr(arvig$community_id, 2, 5), 
                           substr(arvig$community_id, 1, 5))
```


```{r, echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}

arvig$new_column <- as.numeric(substr(arvig$community_id, 1, 5))
# Check data types for specific columns
sapply(arvig[c("kreis", "count", "attack", "demonstration")], class)
# Ensure we're working with the correct data types
arvig$kreis <- as.numeric(arvig$kreis)
arvig$anti <- as.numeric(arvig$count)

arvig$kreis[arvig$kreis == 3159] <- 3158
arvig$kreis[arvig$kreis == 11000] <- 11100
arvig$state_state<-arvig$state

```

Subsetting for the period of interest 1 year previous to the election

```{r, echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
# Ensure that the 'date' column is in Date format
arvig$date <- as.Date(arvig$date)


subset_arvig <- subset(arvig, date >= as.Date("2016-09-01") & date <= as.Date("2017-10-01") )

```

Collapsing by geographic unit

Since the data is a list of events I am aggregating it so I can have an indicator per region. 

```{r, echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
# Aggregating numeric variables
agg_data <- aggregate(cbind(anti, attack, demonstration) ~ kreis, data = subset_arvig, sum, na.rm = TRUE)

arvig_sorted <- subset_arvig[order(subset_arvig$kreis), ]
first_instances <- arvig_sorted[!duplicated(arvig_sorted$kreis), c("kreis", "community_id", "longitude", "latitude", "state_state", "location")]

# Merging aggregated numeric data with first instances of other variables



final_aggregated_data <- merge(agg_data, first_instances, by = "kreis")

# Replace specific 'kreis' values



```


Saving the data I will use to merge with others sources

```{r, echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}

# Get the directory of the current Rmd file
#current_dir <- dirname(rstudioapi::getActiveDocumentContext()$path)

# Define the path to the "Publication" folder
#publication_folder <- normalizePath(file.path(current_dir, ".."))

# Set the working directory to the "Publication" folder
#setwd(publication_folder)

# Verify the working directory has been set correctly
#print(getwd())

# Load the haven package
library(haven)

# Use write_dta to save the dataframe as a .dta file
write_dta(final_aggregated_data, "Data/Region_Germany/final_aggregated_data.dta")
```





#### [3_1_Regional_Germany.do]


This do-file:

- Creates Table 4 and A16 using data from electoral perfomance of the AfD, hate incidents, and exposure to automation. 

Input:

- `Data\Region_Germany\btw2017kreis (3).csv` // This file contains electoral results. 
- `Data\Region_Germany\ RegionEntries14.dta` // This file contains replication data from  "Trade and Manufacturing Jobs in Germany" By Wolfgang Dauth, Sebastian Findeisen, and Jens Suedekum
- `Data\Region_Germany\final_aggregated_data.dta` // This file contains hate incidents in Germany, subset prepared from ARVIG data. The rmd which creates this file is named as  `3_0_Regional_Germany_HateIncidents.rmd`

Alternatively you can go to line 79 and use prepared data: 
  - `Data\Regional_Germany.dta`

Output:

- Table 4: AfD Performance [`Table\AfD_high_pop_r2.tex`]
- Table A16: Summary statistics of variables used in this study about AfD regional performance [`Table\AfD_descriptive.tex`]
 	
 	
```{stata, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE}

* Merging data 
{
// Electoral data
{
import delimited "Data\Region_Germany\btw2017kreis (3).csv", varnames(5) clear 
drop if _n == 1
destring wahlberechtigte wähler ungültige gültige cdu spd dielinke grüne csu fdp afd npd freiewähler bp volksabstimmung pdv mlpd büso sgp dierechte tierschutzallianz b dkp diegrauen du mg menschlichewelt gesundheitsforschung vpartei dievioletten familie diefrauen mieterpartei neueliberale unabhängige übrige, replace

gen afp_prop = afd /gültige // valid vote

rename statistischekennziffer kreis
}
// Regional exposure of workers - Dauth et al 
{ 
merge 1:1 kreis using "Data\Region_Germany\RegionEntries14.dta"

drop if _merge<3 
drop _merge
}
// Hate incidents Arvig
{
merge 1:1 kreis using "Data\Region_Germany\final_aggregated_data.dta", force
 
replace anti=0 if anti==.
}
// Preparing clean file 
{
keep afd gültige afp_prop kreis anti routine  pop  perc_hq perc_foreign perc_female  perc_manuf_trad_nocars perc_manuf_auto  reg_south reg_east reg_north   state_n 

lab var perc_hq "Employment share of workers with University degree (\%)"
lab var perc_foreign "Employment share of Foreign Born (\%)"
lab var perc_female  "Employment share of Female (\%)"
lab var perc_manuf_trad_nocars "Employment share of other manuf. (\%)"
lab var  perc_manuf_auto "Employment share of manuf. of cars (\%)"
lab var afp_prop "AfD Share of votes"
lab var reg_south "South Region"
lab var reg_east "East Region"
lab var reg_north "North Region"
lab var anti "\# Hate incidents per district"
lab var routine "\# Routine workers"
lab var pop "Population"
lab var state_n "State number"

save "Data\Regional_Germany.dta", replace
}
}


```


```{stata, include=TRUE, echo=TRUE, warning=FALSE, message=FALSE, collectcode=TRUE}
*Calling the data
use "Data\Regional_Germany.dta", clear
```

```{stata, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE, collectcode=TRUE}
*******************************************************************************
* Preparing variables
*******************************************************************************
{
gen rou_pop=(routine/pop)
gen anti_pop=(anti/pop)*1000
gen interaction_pop=anti_pop*rou_pop

lab var rou_pop "Share of  exposed workers"
lab var anti_pop "Hate Incidents Per 1K Pop"
lab var interaction_pop "Exposed x Hate"

global controls perc_hq perc_foreign perc_female  perc_manuf_trad_nocars perc_manuf_auto 

}

```

##### Table 4: AfD Performance

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*******************************************************************************
* Analysis
*******************************************************************************
// Table 4: AfD Performance


eststo clear

eststo: qui reg afp_prop rou_pop anti_pop , cluster(state_n) 

eststo: qui reg afp_prop rou_pop anti_pop perc_foreign $controls reg_south reg_east reg_north i.state_n, cluster(state_n) 

eststo: qui reg afp_prop rou_pop anti_pop interaction_pop     reg_south reg_east reg_north perc_foreign  $controls, cluster(state_n)
eststo: qui reg afp_prop rou_pop anti_pop interaction_pop     reg_south reg_east reg_north  i.state_n perc_foreign $controls, cluster(state_n)


esttab , replace label se title(AfD Performance \label {TableAfd})   compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rou_pop anti_pop interaction* )     indicate( "Other controls = perc_foreign" "FE State = *state_n" ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) nomtitle collabels(none) 


	
```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE}
esttab using "Table\AfD_high_pop_r2.tex", replace label se title(AfD Performance \label {TableAfd})   compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rou_pop anti_pop interaction* )     indicate( "Other controls = perc_foreign" "FE State = *state_n" ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) nomtitle collabels(none) 
```


```{=latex}
% Temporarily change table numbering
\renewcommand{\thetable}{4}

% Include the table with custom numbering
\input{Table/AfD_high_pop_r2.tex}

% Reset table numbering to default (optional)
\renewcommand{\thetable}{\arabic{table}}
```

##### Table  A16: Summary statistics of variables used in this study about AfD regional performance


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
////////////////////////////////////
* Descriptives
///////////////////////////////////


eststo clear

// table A16: Summary statistics of variables used in this study about AfD regional performance
{
qui estpost sum afp_prop rou_pop anti_pop  anti    reg_south reg_east reg_north  $controls, d

}

```

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, eval=TRUE}

esttab , /// 
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(2)) max(label(Max) fmt(2)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs
eststo clear

esttab using "Table\AfD_descriptive.tex", /// 
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(2)) max(label(Max) fmt(2)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs
eststo clear

```



```{=latex}
\renewcommand{\thetable}{A16}


        \input{Table/AfD_descriptive.tex}

\renewcommand{\thetable}{\arabic{table}}

```



# Appendix Figures

## [4_1_Figures_ISSP.do]



```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Load  data
*##########################################

use "Data\Figures_ISSP.dta", clear 
	
```



### Figure A3: Importance of job security, Difficulties to find a new job, Concerns about losing the job and Job dissatisfaction 

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, results='hide', collectcode=TRUE}

// Figure A3: Importance of job security, Difficulties to find a new job, Concerns about losing the job and Job dissatisfaction
{
* The code categorizes individuals based on whether they find it difficult to get a new job, then runs a logistic regression to assess how the risk of automation influences this difficulty, adjusting for weights. It calculates the predicted probabilities of job difficulty across different levels of automation risk and generates a graph to visualize these probabilities, saving the result as a file.
* Code commented for first graph whether it is difficult to find a new job. Then code for the other three is similar (levels of variables changes so the dummies are different). 


* Graph style for figure A3
{
grstyle clear
set scheme s2color
grstyle init
grstyle set plain, nogrid
grstyle color background white
}
* W_easynewjob
{
* Generate a new variable 'W_easynewjob_difficult' to categorize the difficulty of finding a new job
* Set 'W_easynewjob_difficult' to 1 if 'W_easynewjob' is 4 or 5 (indicating it is difficult)
gen W_easynewjob_difficult = 1 if W_easynewjob == 4 | W_easynewjob == 5
* Replace 'W_easynewjob_difficult' with 0 if 'W_easynewjob' is 1, 2, or 3 (indicating it is not difficult)
replace W_easynewjob_difficult = 0 if W_easynewjob == 1 | W_easynewjob == 2 | W_easynewjob == 3

* Run a logistic regression model with 'W_easynewjob_difficult' as the dependent variable
logit W_easynewjob_difficult rti [pweight=weight]

* Calculate the marginal effects of 'rti' on the predicted probability of 'W_easynewjob_difficult'
* atmeans calculates the marginal effect at the means of the covariates
* at() specifies a range of values for 'rti' from -1.52 to 2.24 with increments of 0.05
margins, atmeans at(rti=(-1.52(0.05)2.24))

* Plot the margins with a line graph and display the confidence intervals as dotted lines
 margins, atmeans at(rti=(-1.52(0.05)2.24)) 
     marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation) yti("Predicted probability (95% CI)") ti("Job dissatisfaction")  saving("Figure/difficult.gph", replace)
}

	 
* W_satisfaction
{
gen dissatisfied=1 if W_satisfaction==5 | W_satisfaction==6 | W_satisfaction==7
replace dissatisfied=0 if W_satisfaction==1 | W_satisfaction==2 | W_satisfaction==3 | W_satisfaction==4


logit dissatisfied rti [pweight=weight]

 margins, atmeans at(rti=(-1.52(0.05)2.24)) 
     marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation) yti("Predicted probability (95% CI)") ti("Job dissatisfaction")  saving("Figure/dissatisfied.gph", replace)
}	 

	 
* W_losing
{
gen W_losing2=1 if W_losing==1 | W_losing==2 | W_losing==3
replace W_losing2=0 if W_losing==4 


logit W_losing2   rti [pweight=weight]

 margins, atmeans at(rti=(-1.52(0.05)2.24)) 
     marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation) yti("Predicted probability (95% CI)") ti("Concerns about losing job") saving("Figure/losing.gph", replace)
}

* W_jobsec
{
gen W_jobsec2=1 if W_jobsec==1 | W_jobsec==2
replace W_jobsec2=0 if W_jobsec==3 | W_jobsec==4 | W_jobsec==5


logit W_jobsec2 rti [pweight=weight] 
 margins, atmeans at(rti=(-1.52(0.05)2.24)) 
     marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation) yti("Predicted probability (95% CI)") ti("Importance of job security") saving("Figure/security.gph", replace)
}
* Combine the specified graphs into one figure
graph combine "Figure/security.gph" "Figure/difficult.gph" "Figure/losing.gph" "Figure/dissatisfied.gph"

* Export the combined graph as a PDF file, replacing any existing file
graph export "Figure/jobdisatisfactionpredictedtogetherall.pdf", as(pdf) replace

* Delete the individual graph files after combining them
erase "Figure/security.gph"
erase "Figure/difficult.gph"
erase "Figure/losing.gph"
erase "Figure/dissatisfied.gph"

}

```





![Figure A3: Importance of job security, Difficulties to find a new job, Concerns about losing the job and Job dissatisfaction ](Figure\\jobdisatisfactionpredictedtogetherall.pdf){ width=12cm }


## [4_2_Figures_Appendix_ESS.do]


This do-file:

- Creates Figure A2 using data from the ESS. 

Input:

- `Data\Appendix_ESS.dta`

Output:

- Figure A2: Share routine and non-routine 2002-2018 [`Figure\Share routine and non routine.pdf`]


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Load  data
*##########################################

use "Data\Appendix_ESS.dta", clear 
	
```



### Figure A2: Share routine and non-routine 2002-2018

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}

*******************************************************************************
* Graphs
*******************************************************************************
* Graph style
{
grstyle clear
set scheme s2color
grstyle init
grstyle set plain, box
grstyle color background white
grstyle color major_grid gs8
grstyle linepattern major_grid dot
}
// Figure A2: Share routine and non-routine 2002-2018
{
preserve
// Preserve the current dataset in memory to allow restoration later

// Generate a binary variable 'tokeep' to identify the countries to be kept for Figure A2
gen tokeep = (cou == "BEL" | cou == "CZE" | cou == "EST" | cou == "HUN" | cou == "LUX" | cou == "SVK" | cou == "SVN" | cou == "DNK" | cou == "FIN" | cou == "FRA" | cou == "DEU" | cou == "GRC" | cou == "IRL" | cou == "NLD" | cou == "PRT" | cou == "ESP" | cou == "SWE" | cou == "GBR" | cou == "NOR" | cou == "CHE" | cou == "AUT" | cou == "ITA" | cou == "POL")

// Keep only the observations for the countries specified in 'tokeep'
keep if tokeep == 1


// Keep observations where 'mnactic' is 1 or 3 (paid work or unemployed looking for a job), and 'rti' is not missing
keep if inlist(mnactic, 1, 3) & rti ~= .

// Generate a binary variable 'routine' to indicate whether the task is routine (rti > 0)
gen routine = 1 if rti > 0
replace routine = 0 if rti < 0

// Calculate the total number of people in each occupation per year for each country and routine category
bysort year routine cou: egen empl = sum(dweight)

// Calculate the total number of people in each ISCO2 category per year for each country
bysort year cou: egen tot = sum(dweight)

// Calculate the share of people within each type of task (routine or non-routine) per year
gen share = empl / tot * 100

// Label the variables with descriptive names
label var empl "total no people within occup/year"
label var tot "total no people with isco2 within year"
label var share "share of people within type of task routinary or not /year"
lab var cou "Country"

// Collapse the dataset to calculate the weighted share of routine and non-routine tasks by year and country
collapse share [aw=pweight], by(routine year cou)

// Sort the dataset by country, year, and routine status
sort cou year routine share

// Generate separate variables for the share of non-routine and routine tasks
gen routine0 = share if routine == 0
gen routine1 = share if routine == 1

// Calculate the total share of non-routine tasks per year and country
bysort year cou: egen share0 = total(routine0)

// Calculate the total share of routine tasks per year and country
bysort year cou: egen share1 = total(routine1)

// Calculate the polarization between non-routine and routine tasks
gen polarization = share0 - share1

// Generate a line graph showing the share of routine and non-routine tasks over time for each country
graph twoway ///
    line share year if routine == 1, lc(red) legend(label(1 "Routine")) by(cou) || ///
    line share year if routine == 0, lc(blue) legend(label(2 "Non-Routine")) by(cou)

// Export the graph to a PDF file
graph export "Figure\Share routine and non routine.pdf", as(pdf) replace

// Restore the original dataset that was in memory before any modifications
restore
	
}
```





![Figure A2: Share routine and non-routine 2002-2018 ](Figure\\Share routine and non routine.pdf){ width=12cm }

## [4_3_Figures_Appendix_CHES.do]

This do-file:

- Creates Figure A4 using data from CHES. 

Input:

- `Data\1999-2019_CHES_dataset_means(v3).dta`

Output:

- Figure A4: Number of Radical Right Parties in the Party System [`Figures\NewParty.pdf`]



```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Load  data
*##########################################

use "Data\1999-2019_CHES_dataset_means(v3).dta", clear 
	
```



### Figure A4: Number of Radical Right Parties in the Party System

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*******************************************************************************
* Preparing variables
*******************************************************************************
{
gen radright=1 if family==1
replace radright=0 if family>1
}

******************************************************************************
* Graph
******************************************************************************
* Graph style 
{
grstyle clear
set scheme s2color
grstyle init
grstyle set plain, box
grstyle color background white
grstyle set color dknavy
grstyle yesno draw_major_hgrid yes
grstyle yesno draw_major_ygrid yes
grstyle color major_grid gs8
grstyle linepattern major_grid dot
grstyle color ci_area gs12%50
 graph set window fontface "Georgia"
}
// Figure A4: Number of Radical Right Parties in the Party System
{
preserve
collapse (sum) radright, by(year)  // Collapse the data by summing the 'radright' variable for each year

line radright year, ytitle("Number of Radical Right Parties", size(small))   xtitle("Year", size(small))  
graph export "Figure\NewParty.pdf", as(pdf) replace

restore
}
```






![Figure A4: Number of Radical Right Parties in the Party System](Figure\\NewParty.pdf){ width=12cm }

## [4_4_Figures_Appendix_CMP.do]

This do-file:

- Creates Figure A5 using data from ISSP. 

Input:

- `Data\CMP\MPDataset_MPDS2020a_stata14.dta`

Output:

-  Figure A5: Number of Nationalist Parties in Elections [`Figures\Nationalist.pdf`]


```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Load  data
*##########################################

use "Data\CMP\MPDataset_MPDS2020a_stata14.dta", clear 
	
```



### Figure A5: Number of Nationalist Parties in Elections

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*******************************************************************************
* Preparing variables
*******************************************************************************
{
gen year = year(edate) 

keep if countryname=="Australia" |  countryname=="Canada" |  countryname=="Greece" |  countryname=="New Zealand" |  countryname=="Portugal" |  countryname=="Spain" |  countryname=="United Kingdom" |  countryname=="United States" |  countryname=="France" |  countryname=="Norway" | countryname=="Austria" | countryname=="Belgium" | countryname=="Denmark" | countryname=="Estonia" | countryname=="Finland" | countryname=="Hungary" | countryname=="Germany" | countryname=="Iceland"  | countryname=="Ireland"  | countryname=="Italy"  | countryname=="Netherlands"  | countryname=="Norway" | countryname=="Slovakia" | countryname=="Slovenia" | countryname=="Sweden" | countryname=="Switzerland"

gen radright=1 if parfam==70
replace radright=0 if parfam>70 | parfam<70
}

*******************************************************************************
* Graphs
*******************************************************************************
* Graph style
{
grstyle clear
set scheme s2color
grstyle init
grstyle set plain, box
grstyle color background white
grstyle set color dknavy
grstyle yesno draw_major_hgrid yes
grstyle yesno draw_major_ygrid yes
grstyle color major_grid gs8
grstyle linepattern major_grid dot
grstyle color ci_area gs12%50
 graph set window fontface "Georgia"
}
// Figure A5: Number of Nationalist Parties in Elections
{
sort year  // Sort the dataset by year
collapse (sum) radright, by(year)  // Collapse the data by summing the 'radright' variable for each year
tsset year  // Declare the dataset to be time-series data with 'year' as the time variable

rolling, window(4) saving(rolling_dataset, replace): egen ma_radright = total(radright)  // Calculate a 4-year moving average of the 'radright' variable and save the result in 'rolling_dataset.dta'

use "rolling_dataset.dta", clear  // Load the 'rolling_dataset.dta' file
line mean start if start > 1969, title("Number of Nationalist Parties with 4-Yr Moving Average") xtitle("Year") ytitle("Number of Nationalist Parties")  // Create a line graph of the 4-year moving average of nationalist parties starting from 1970
graph export "Figure\Nationalist.pdf", as(pdf) replace  // Export the graph to a PDF file and replace any existing file with the same name

* Drop the .dta file
erase "rolling_dataset.dta"  // Delete the 'rolling_dataset.dta' file from the directory

}

```





![Figure A5: Number of Nationalist Parties in Elections](Figure\\Nationalist.pdf){ width=12cm }



## [4_5_Figures_Appendix_StockRobots.do]


This do-file:

- Creates Figure A1 using data from Acemoglu & Restrepo. 

Input:

- `Data\reproducingacemoglu.csv`

Output:

- Figure A1: Stock of robots per thousand of workers base 1993 [`Figures\number_robots.pdf`]

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}
*##########################################
* Load  data
*##########################################

 import delimited "Data\reproducingacemoglu.csv", clear
	
```



### Figure A1: Stock of robots per thousand of workers base 1993

```{stata,  echo=TRUE, include=TRUE, message=FALSE, warning=FALSE, collectcode=TRUE}

******************************************************************************
* Graph setting
******************************************************************************
* Graph style 
{
grstyle clear
set scheme s2color
grstyle init
grstyle set plain, box
grstyle color background white
grstyle set color dknavy
grstyle yesno draw_major_hgrid yes
grstyle yesno draw_major_ygrid yes
grstyle color major_grid gs8
grstyle linepattern major_grid dot
grstyle color ci_area gs12%50
 graph set window fontface "Georgia"
}
 //Figure A1: Stock of robots per thousand of workers base 1993
 {
twoway ///
    (line germany year, lcolor(blue) lwidth(medium) lpattern(solid)) ///
    (line denmarkfinlandfranceitalyandswed year, lcolor(gs10) lwidth(medium) lpattern(longdash)) ///
    (line unitedstates year, lcolor(blue) lwidth(medium) lpattern(shortdash)) ///
    (line norwayspainanduk year, lcolor(blue) lwidth(medium) lpattern(dash_dot)), ///
    xtitle("Year") ytitle("Stock of robots per thousand of workers") ///
    legend(order(1 "Germany" 2 "Denmark, Finland, France, Italy and Sweden" 3 "United States" 4 "Norway, Spain, and UK") position(6) size(small)) ///
    yscale(range(0 6)) xscale(range(1993 2014)) legend(size(small))


graph export "Figure\number_robots.pdf", as(pdf) replace
}

```





![Figure A1: Stock of robots per thousand of workers base 1993](Figure\\number_robots.pdf){ width=12cm }
